[Tutor] how to access deep classes

Alan Gauld alan.gauld at btinternet.com
Fri Nov 20 18:48:38 CET 2009

"John" <jfabiani at yolo.com> wrote

> class A (wx.Panel);
>   def__init__(...)
> class B(wx.PyPanel):
>   def __init__(..):
>     self.pages = A(...)
> class C (B)
>  def __init__(...)
> I can't change the code in either class A or class B.  But I want to add 
> a
> property to class A in class C.  Is that possible?

You need to distinguish between classes and instances thereof.

You can add attributes to instances of A, and you can access the
instance because it is stored in self.pages of C. (You can actually add
attributes and methods to the class too, after it is defined but I wouldn't
recommend that since it gets  very confusing if you duplicate a name
added to an instance elsewhere!)

So you can, within C's methods do

self.pages.foo = 66

which adds a new foo attribute to the instance of A stored in pages.
Just be careful you don't have any code anywhere that relies on the
content of A instances (eg pickle/unpickle type things).

> wxpanelFontSize = property(_getwxpanelFontSize, _setwxpanelFontSize, 
> None, '')

Sorry, the connection to A is not clear.

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list