super problem

Michele Simionato mis6 at pitt.edu
Fri Jan 17 17:16:17 EST 2003


I am doing the following:

  >>> class B(object): a='attribute B.a'
  >>> class C(B):  a='attribute C.a'
  >>> parent=C.mro()[1]
  >>> setattr(parent,'a',"I have changed 'a' in the parent of C")
  >>> B.a
  "I have changed 'a' in the parent of C"

It works, but I wonder if there is a better way to do that.
I thought this was a job for 'super', but 

   setattr(super(C),'a',"I have changed 'a' in the parent of C")

doesn't work. It seems that super doesn't give the superclass, but a 
"super object" that I don't find in the documentation. How do I extract
the parent class from the super object ? Or maybe the .mro() trick
is the right way to go, after all ?
Also, how do I access the grandfather class ? With the .mro() it is
obviously .mro()[2], but with super I don't now if I can chain
two super or not.
TIA,

--
Michele Simionato - Dept. of Physics and Astronomy
210 Allen Hall Pittsburgh PA 15260 U.S.A.
Phone: 001-412-624-9041 Fax: 001-412-624-9163
Home-page: http://www.phyast.pitt.edu/~micheles/




More information about the Python-list mailing list