1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python调用父类构造函数需要放在第一句吗_Python继承和调用父类构造函数

python调用父类构造函数需要放在第一句吗_Python继承和调用父类构造函数

时间:2023-01-25 04:38:12

相关推荐

python调用父类构造函数需要放在第一句吗_Python继承和调用父类构造函数

这是我正在Python中做的:

class BaseClass:

def __init__(self):

print 'The base class constructor ran!'

self.__test = 42

class ChildClass(BaseClass):

def __init__(self):

print 'The child class constructor ran!'

BaseClass.__init__(self)

def doSomething(self):

print 'Test is: ', self.__test

test = ChildClass()

test.doSomething()

其结果如下:

AttributeError: ChildClass instance has no attribute '_ChildClass__test'

是什么赋予了?为什么不按照我的预期工作?

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。