"parent" in a class __init__ def?

akameswaran at gmail.com akameswaran at gmail.com
Sun Jun 11 00:16:49 EDT 2006


I'm not sure how it's a comparison to class variables.  So I wouldn't
worry about that.  I think there are some advantages to having the
parent as an instance member.  Intuitively, the name lookup on
self.parent.foo would be faster than if you passed in the object in
question - although I haven't tested this.  In short, there's nothin
wrong with doin it - and it helps in many situations.



Ray Schumacher wrote:
> What is the feeling on using "parent" in a class definition that
> class methods can refer to, vs. some other organization ?
> Should all relevant objects/vars just be passed into the method as needed?
> It seems like including "parent" in the class def is just like a
> class variable, which most do not recommend.
>
> An example:
> class LXSerial:
>      def __init__(self, parent, debug=False):
>      ...
>      def connect(self, port, baud=9600, ptimeout=10):
>          if self.debug:
>              self.connectedPort = StringIO.StringIO(':A#')
>          else:
>              if self.parent.model=='LX200GPS': ptimeout = 240
>              ...
> 
> Ray




More information about the Python-list mailing list