[Tutor] how to access deep classes

John jfabiani at yolo.com
Fri Nov 20 23:14:45 CET 2009


On Friday 20 November 2009 09:48:38 am Alan Gauld wrote:
> "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.

Thanks - for your help.

Johnf


More information about the Tutor mailing list