多继承super() [英] multiple inheritance super()
问题描述
大家好,
在下面的代码中,为什么我无法访问A类的对象属性 - ''a''?我希望用它的基类的所有属性来扩展D类。我是怎么做到的?
提前感谢您的启发...
这里的'片段
#!/ usr / bin / python
A类(对象):
def __init __(自我):
self.a = 1
B级(对象):
def __init __(个体经营):
self .b = 2
C级(对象):
def __init __(自我):
self.c = 3
D级(B,A,C):
def __init __(个体经营):
self.d = 4
super(D,self).__ init __()
if __name__ ==''__ main__'':
x = D()
打印xa #errs with - AttributeError
km写道:大家好,
在下面的代码中,为什么我无法访问A类的对象属性 - ''a''?我希望用它的基类的所有属性来扩展D类。我该怎么做?
提前感谢你们的启发...
这里的代码片段
#!/ usr / bin / python
A类(对象):
def __init __(self):
self.a = 1
B类(对象):
def __init __(self):
self.b = 2
类C(对象):
def __init __(self):
self.c = 3类(B,A,C):
def __init __(自我):
self.d = 4
super(D,self).__ init__ ()
每个班级应该进行类似的super()调用,并使用适当的名称
替换。
>
必须在子类中明确调用__init__,包括
多重继承的情况。
另请注意,经常(通常)你希望在*其他位置初始化之前__init__来电话来获得
*,这是最安全的事情
除非你有明确的理由c ontrary。
-Peter
嗨彼得,
你做得好吗:-)现在我理解mro更好。
谢谢,
KM
---------- -------------------------------------------------- -
On Tue,2005年7月26日下午04:09:55 -0400,Peter Hansen写道:km写道:大家好,
在下面的代码中,为什么我无法访问A类的对象属性 - ''a''?我希望用它的基类的所有属性来扩展D类。我该怎么做?
提前感谢你们的启发...
这里的代码片段
#!/ usr / bin / python
A类(对象):
def __init __(self):
self.a = 1
B类(对象):
def __init __(self):
self.b = 2
类C(对象):
def __init __(self):
self.c = 3类(B,A,C):
def __init __(自我):
self.d = 4
super(D,self).__ init__ ()
每个类都应该执行类似的super()调用,并使用适当的名称替换。
必须明确调用__init__在子类中,包括在多重继承的情况下。
另请注意,在*其他位置初始化之前,通常(通常)您希望__init__调用来*
*这是最安全的事情
除非你有明确的理由相反。
-Peter
- http://mail.python.org/mailman/listinfo / python-list
Peter Hansen写道:km写道:
大家好,
在下面的代码中,为什么我无法访问A类的对象
属性 - ''a''?我希望用它的基类的所有属性来扩展D类。我该怎么做?
[snip]每个类都应该进行类似的super()调用,并使用相应的名称替换。
[snip] -Peter
相关问题是关于__init__调用的顺序。考虑
以下样本:
# - 8< ---
A类(对象):
def __init__(个体经营):
super(A,self).__ init __()
打印''我是A''
def foo(个体经营):
打印''A.foo''
B级(对象):
def __init__(个体经营):
super(B,self).__ init __()
打印''我是B''
def foo(个体经营):
打印''B.foo''
C级(A,B):
def __init __(自我):
super(C,self).__ init __()
打印''我是C''
c = C()
c.foo()
# - 8< ---
aerts
Hi all,
In the following code why am i not able to access class A''s object attribute - ''a'' ? I wishto extent class D with all the attributes of its base classes. how do i do that ?
thanks in advance for enlightment ...
here''s the snippet
#!/usr/bin/python
class A(object):
def __init__(self):
self.a = 1
class B(object):
def __init__(self):
self.b = 2
class C(object):
def __init__(self):
self.c = 3
class D(B, A, C):
def __init__(self):
self.d = 4
super(D, self).__init__()
if __name__ == ''__main__'':
x = D()
print x.a # errs with - AttributeError
km wrote:Hi all,
In the following code why am i not able to access class A''s object attribute - ''a'' ? I wishto extent class D with all the attributes of its base classes. how do i do that ?
thanks in advance for enlightment ...
here''s the snippet
#!/usr/bin/python
class A(object):
def __init__(self):
self.a = 1
class B(object):
def __init__(self):
self.b = 2
class C(object):
def __init__(self):
self.c = 3
class D(B, A, C):
def __init__(self):
self.d = 4
super(D, self).__init__()
Each class should do a similar super() call, with the appropriate name
substitutions.
Calls to __init__ must be made explicitly in subclasses, including in
the case of multiple inheritance.
Also note that often (usually) you would like the __init__ call to come
*before* other location initializations, and it''s the safest thing to do
unless you have clear reasons to the contrary.
-Peter
Hi peter,
ya got it working :-) now i understand mro better.
thanks,
KM
-------------------------------------------------------------
On Tue, Jul 26, 2005 at 04:09:55PM -0400, Peter Hansen wrote:km wrote:Hi all,
In the following code why am i not able to access class A''s object attribute - ''a'' ? I wishto extent class D with all the attributes of its base classes. how do i do that ?
thanks in advance for enlightment ...
here''s the snippet
#!/usr/bin/python
class A(object):
def __init__(self):
self.a = 1
class B(object):
def __init__(self):
self.b = 2
class C(object):
def __init__(self):
self.c = 3
class D(B, A, C):
def __init__(self):
self.d = 4
super(D, self).__init__()
Each class should do a similar super() call, with the appropriate name
substitutions.
Calls to __init__ must be made explicitly in subclasses, including in
the case of multiple inheritance.
Also note that often (usually) you would like the __init__ call to come
*before* other location initializations, and it''s the safest thing to do
unless you have clear reasons to the contrary.
-Peter
--
http://mail.python.org/mailman/listinfo/python-list
Peter Hansen wrote:km wrote:Hi all,
In the following code why am i not able to access class A''s object
attribute - ''a'' ? I wishto extent class D with all the attributes of
its base classes. how do i do that ?[snip] Each class should do a similar super() call, with the appropriate name
substitutions. [snip] -Peter
A related question is about the order of the __init__ calls. Considering
the following sample:
#--8<---
class A (object):
def __init__ (self):
super (A, self) .__init__ ()
print ''i am an A''
def foo (self):
print ''A.foo''
class B (object):
def __init__ (self):
super (B, self) .__init__ ()
print ''i am a B''
def foo (self):
print ''B.foo''
class C (A, B):
def __init__ (self):
super (C, self) .__init__ ()
print ''i am a C''
c = C ()
c.foo ()
#--8<---
aerts
这篇关于多继承super()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!