Created
June 5, 2020 05:21
-
-
Save ZhouYang1993/61fa29341b5c92d2732c2aaee3015568 to your computer and use it in GitHub Desktop.
Understand Constructors in Python Classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Student(object): | |
| def __new__(cls, *args, **kwargs): | |
| print("Running __new__() method.") | |
| return None | |
| def __init__(self, first_name, last_name): | |
| print("Running __init__() method.") | |
| self.first_name = first_name | |
| self.last_name = last_name | |
| s1 = Student("Yang", "Zhou") | |
| print(s1.last_name) | |
| # Running __new__() method. | |
| # AttributeError: 'NoneType' object has no attribute 'last_name' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment