Re: Class variable inheritance
> Makes sense to me. To step through what's happening: > > >>> A.n, B.n > (0, 0) > > Here, the lookup on B.n fails (that is, B itself has no variable n), > and thus falls back to A.n See, this is what tripped me up, right at the beginning. I thought B would inherit (as in copy) the variable n from A. Can you point out to me which part (or parts) of the Language Reference says this is the way it's supposed to be? -- http://mail.python.org/mailman/listinfo/python-list
Re: Class variable inheritance
> I could, but I will let you read and find what it says about class > attributes. You think I would have asked specifically about the Language Reference if I hadn't read it and failed to find what I was looking for? The closest thing I was able to find was section 3.2. "The standard type hierarchy", subsection "Custom classes", where it says: "When the attribute name is not found [in its namespace dictionary], the attribute search continues in the base classes. This search of the base classes uses the C3 method resolution order [...]" That tells me how the lookup works, which I already knew. But it doesn't tell me what happens when a class is inherited. Now, one could argue if the attributes were copied, there would be no need for a lookup in the base classes, so derivatively it says that's not the case. To this I would say yes, there still would because after they've been copied through inheritance, attributes can still be removed via "del". Anyway, this passage, which is the only one I could find, doesn't provide a direct and conclusive answer to my question. -- http://mail.python.org/mailman/listinfo/python-list
Re: Class variable inheritance
> would you expect the B class to have a copy of the foo method? Sorta. I would expect B to have a copy of the "foo" attribute, which then refers to the same method as A.foo. So the method itself will be be copied, but its address stored separately in A.foo and B.foo. -- http://mail.python.org/mailman/listinfo/python-list
Re: Class variable inheritance
> Conceptually, Python checks for the presence of B.foo, and if it's > not there it checks for foo's presence in the base classes. Yes, I have no problem believing you guys that this is what Python does. Still, my question remains about where in the Language Reference this is specified. And if the answer is nowhere, than the LR needs to be amended, for obviously the way inheritance is done is no small matter and its understanding should not be left to the user's own intuition. -- http://mail.python.org/mailman/listinfo/python-list
Re: Class variable inheritance
> http://docs.python.org/reference/datamodel.html#new-style-and-classic... > - search for 'method resolution order' for other hits in that document. First of all, that's the LR for Python 2, I'm with Python 3. Second of all, there's one single passage containing the phrase "method resolution order" in the Python 3 LR, and it's one that I've already quoted -- and discarded -- previously in this thread. -- http://mail.python.org/mailman/listinfo/python-list
Re: Class variable inheritance
And by the way, the reason I've come across this problem at all is because I have something like this: class A: class X: n = 'a' x = X() class B(A): class X(A.X): n = 'b' # x = X() The line commented out was originally not there, but I found out I had to add it if I wanted B().x.n to be 'b' instead of 'a'. I might be wrong, but aren't there some (non-obscure) OOP language in which the equivalent code (without the out-commented line) would have made B().x an object of type B.X instead of A.X? -- http://mail.python.org/mailman/listinfo/python-list
Re: Class variable inheritance
> Maybe. For some languages this might be an obvious behavior, but > Python would have different circumstances so it isn't so obvious (or > possible) there. Which means this topic definitely needs to be handled in detail by the LR, and preferably also in the Tutorial. Otherwise there is no way anyone coming to Python could know which of the many ways of inheritance Python follows. -- http://mail.python.org/mailman/listinfo/python-list