Skip to content

Instantly share code, notes, and snippets.

@ZhouYang1993
Created June 5, 2020 05:21
Show Gist options
  • Save ZhouYang1993/61fa29341b5c92d2732c2aaee3015568 to your computer and use it in GitHub Desktop.
Save ZhouYang1993/61fa29341b5c92d2732c2aaee3015568 to your computer and use it in GitHub Desktop.
Understand Constructors in Python Classes
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