来自stackoverflow的一个关于 python 嵌套类的问题(nested class)
来源:互联网 发布:毒品是谁发明的知乎 编辑:程序博客网 时间:2024/05/17 06:10
http://stackoverflow.com/questions/8775246/nested-classes-in-python
有人在stack overflow上提问关于nested class in Python 的问题:
#------------------------------------class A: def __init__(self): self.a = 'a' print self.aclass B(A): def __init__(self): self.b = 'b' A.a = 'a_b' print self.b, A.a#------------------------------------class C: class A: def __init__(self): self.a = 'a' print self.a class B(A): def __init__(self): self.b = 'b' A.a = 'a_b' print self.b, A.a#------------------------------------#------------------------------------>>> c1 = A()a>>> c1.a'a'>>> c2 = B()b >>> c2.a, c2.b('a_b', 'b')>>> c3 = C()>>> c4 = c3.A()a>>> c4.a'a'>>> c5 = c3.B()b a_b>>> c5.b'b'>>> c5.aTraceback (most recent call last): File "", line 1, in AttributeError: B instance has no attribute 'a'
Where is the problem in the code? AND In both cases it seems that when B(A) is initialized A() is not initialized. What is the solution for this issue? Note that the term A.__init__()
being called inside B()'s __init__()
does not work!前面的c2.a, c2.b 都可以好好的工作,怎么c5.b就挂了呢?
下面是2个有用的回答:
answer 1:
The code executed in a method runs in the local scope of that method. If you access an object that is not in this scope, Python will look it up in the global/module scope, NOT in the class scope or the scope of any enclosing class!
This means that:
A.a = 'a_b'
inside C.B.__init__
will set the class attribute of the global A
class, not C.A
as you probably intended. For that you would have to do this:
C.A.a = 'a_b'
Also, Python will not call parent methods if you override them in subclasses. You have to do it yourself.
The scoping rules mean that if you wanted to call the __init__
method of the parent class inside C.B.__init__
, it has to look like this:
C.A.__init__(self)
and NOT like this:
A.__init__(self)
which is probably what you've tried.
x = 1def a(): print x x = 2 print x
answer 2:
Nested classes seems so unpythonic, even if considered as factories. But to answer your question: There simply is no c5.a (instance of C.B). In the init-method of C.B you add to the CLASS C.A an attribute a, but not to C.B! The class A does already have an attribute a, if instantiated! But the object of class B (and even the class) doesn't!
You must also keep in mind, that __init__
is not an constructor like in C++ or Java! The "real constructor" in python would be __new__
. __init__
just initializes the instance of a class!
class A: c = 'class-attribute' def __init__(self): self.i = 'instance-attribute'
So in this example c is a class-attribute, where i is an attribute of the instance.
Even more curios, is your attempt to add an attribute to the baseclass at the moment of the instantiation of the child-class. You are not getting a "late" inheritance-attribute that way. You simply add to the class A an additional attribute, which surprises me to even work. I guess you are using python 3.x?
The reason for this behaviour? Well, i guess it has to do with pythons neat feature that in python definitions areexecuted(AFAIK).
The same reason why:
def method(lst = []):
is almost ever a bad idea. the deafult-parameter gets bound at the moment of the definition and you won't generate a new list-object every-time you call the method, but reusing the same list-object.
这里给出了如何解决问题的答案,就是使用显示的调用,比如:C.A.a。另外也说了代码中存在的问题,就是类B中调用A.a其实不是作者的本意,作者用了未绑定方法。answer 2最后还提到了不要用可变类型做函数可选参数了。
最后作者Update了正确方法:
class Geometry: class Curve: def __init__(self,c=1): self.c = c #curvature parameter print 'Curvature %g'%self.c pass #some codes class Line(Curve): def __init__(self): Geometry.Curve.__init__(self,0) #the key point pass #some codesg = Geometry()C = g.Curve(0.5)L = g.Line()
- 来自stackoverflow的一个关于 python 嵌套类的问题(nested class)
- Python关键字yield的解释(来自stackoverflow)
- 关于Python的嵌套域(nested scope)
- StackOverflow上看到的关于返回值一个问题
- Java的内部类(Inner Class)和嵌套类(Nested Class)的区别
- Java的内部类(Inner Class)和嵌套类(Nested Class)的区别
- 嵌套类(Nested Class)
- Static Nested Class(嵌套类) 和 Inner Class(内部类)的不同
- Static Nested Class(嵌套类) 和 Inner Class(内部类)的不同
- 静态嵌套类(Static Nested Class)和内部类(Inner Class)的不同
- 静态嵌套类(Static Nested Class)和内部类(Inner Class)的不同
- 静态嵌套类(Static Nested Class)和内部类(Inner Class)的不同?
- C++ nested class 嵌套类与外围类 访问问题
- Stackoverflow上的Python问题精选
- Stackoverflow上的Python问题精选
- java 中 关于 StackOverflow 的问题
- 【java面试题】Static Nested Class(嵌套类) 和 Inner Class(内部类)的不同 、final、static
- python中关于class类中一个实例变量的引用问题
- 【PHP内核学习】线程安全
- Java学习之Iterator(迭代器)的一般用法
- MIPS虚拟地址到物理地址转换过程
- 从代码放置位置上来寻找问题的答案
- 【链表&删除倒数第K个节点】Remove Nth Node From End of List
- 来自stackoverflow的一个关于 python 嵌套类的问题(nested class)
- OpenCV学习笔记(四十五)——小试随机森林(random forest)算法ml
- ffmpeg参数设置
- 追忆那些逝去的开源人
- 扁平化流行时
- 安卓之实现一个简单的短信发送功能
- 两个字符串之续
- ZOJ Monthly, September 2011(2014省赛练习)
- 查找与散列(Hash)