Question about accessing class-attributes.

Bjorn Pettersen BPettersen at NAREX.com
Thu May 1 01:47:33 EDT 2003


> From: Alex Martelli [mailto:aleax at aleax.it] 

[superclass vs. baseclass]

> I'm not sure what the practical use case for the 2nd argument being 
> a type is:
> 
> >>> class A(object): a = 23
> ...
> >>> class B(object): a = 45
> ...
> >>> class C(A, B): a = 67
> ...
> >>> C.a
> 67
> >>> super(A, C).a
> 45
> 
> but I don't know how one would get, from C, some object whose
> attribute named 'a' is worth 23.  So, I guess I just don't 
> understand this part of super's behavior.

Well, you wouldn't have cooperative super calls without a diamond
hierarchy, so in this case never <wink>.

The use case is of course our old friend, the classmethod:
  
  class T(object):
     def foo(cls):
        print 'T.foo'
     foo = classmethod(foo)
        
  class Left(T):
     def foo(cls):
        super(Left, cls).foo()
        print 'Left.foo'
     foo = classmethod(foo)
        
  class Right(T):
     def foo(cls):
        super(Right, cls).foo()
        print 'Right.foo'
     foo = classmethod(foo)
        
  class B(Left, Right):
     def foo(cls):
        super(B, cls).foo()
        print 'B.foo'
     foo = classmethod(foo)
        
  B.foo()
      
-- bjorn





More information about the Python-list mailing list