Class variable inheritance

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Tue Sep 8 18:53:38 EDT 2009


On 2009年9月08日 04:36:19 -0700, HPJ wrote:
>> 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.

Inherit does not mean copy. What makes you think it does? Given the 
following:
class A(object):
 def foo(self):
 return "foo"
class B(A):
 pass
would you expect the B class to have a copy of the foo method?
-- 
Steven


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /