To begin with, I know there is a right way to implement inheritance like this:
class Parent():
def __init__(self, last_name, eye_color):
self.last_name = last_name
self.eye_color = eye_color
class Child(Parent):
def __init__(self, last_name, eye_color, number_of_toys):
Parent.__init__(self, last_name, eye_color)
self.number_of_toys = number_of_toys
miley_cyrus = Child("Cyrus", "Blue", 5)
print(miley_cyrus.last_name)
print(miley_cyrus.number_of_toys)
When I run the piece of code, there is the result that
Cyrus
5
However when I change the 7th line into :
self = Parent(last_name, eye_color)
and the the code has become:
class Parent():
def __init__(self, last_name, eye_color):
self.last_name = last_name
self.eye_color = eye_color
class Child(Parent):
def __init__(self, last_name, eye_color, number_of_toys):
self = Parent(last_name, eye_color)
self.number_of_toys = number_of_toys
miley_cyrus = Child("Cyrus", "Blue", 5)
print(miley_cyrus.last_name)
print(miley_cyrus.number_of_toys)
, I run the piece of code, there is an error indicates that:
Traceback (most recent call last):
File "/Users/Echo/Documents/IT/Udacity/python/7.Inheritance/inherentance.py", line 12, in <module>
print(miley_cyrus.last_name)
AttributeError: Child instance has no attribute 'last_name'
What's wrong with that? Thanks in advance.