nested classes in Python

前端 未结 2 1677
攒了一身酷
攒了一身酷 2020-12-25 08:38

Dealing with classes (nested etc) does not look easy in Python, surprisingly! The following problem appeared to me recently and took sever

2条回答
  •  旧巷少年郎
    2020-12-25 09:43

    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 are executed(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.

提交回复
热议问题