Я играю с наследованием Python Class и столкнулся с проблемой, когда унаследованный __init__
не выполняется, если вызван из подкласса (код ниже), результат, который я получаю от Active Python:
>>> start
Tom Sneed
Sue Ann
Traceback (most recent call last):
File "C:\Python26\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 312, <br>in RunScript
exec codeObject in __main__.__dict__
File "C:\temp\classtest.py", line 22, in <module>
print y.get_emp()
File "C:\temp\classtest.py", line 16, in get_emp
return self.FirstName + ' ' + 'abc'
AttributeError: Employee instance has no attribute 'FirstName'
Вот код
class Person():
AnotherName = 'Sue Ann'
def __init__(self):
self.FirstName = 'Tom'
self.LastName = 'Sneed'
def get_name(self):
return self.FirstName + ' ' + self.LastName
class Employee(Person):
def __init__(self):
self.empnum = 'abc123'
def get_emp(self):
print self.AnotherName
return self.FirstName + ' ' + 'abc'
x = Person()
y = Employee()
print 'start'
print x.get_name()
print y.get_emp()