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 > > Passing parent instance into a class is perfectly legal and is used extensively in modules like wxPython GUI. It isn't really anything like a class variable as the instance is normally passed not the class itself. Each instance can have different attributes. So if you have many parents with many children this can be an effective way to structure them.
I think it depends on how deeply nested things get and how many parameters need to be passed. I've used it when I want to nest my objects more than 2 deep and I must pass around lots of attributes. I find it is easier to just look "upwards" into the parent to get the attribute than to clutter up my argument list passing arguments deeper and deeper into the class hierarchy. It can simplify the argument lists quite a bit. Maybe others can comment with their thoughts as well. -Larry Bates -- http://mail.python.org/mailman/listinfo/python-list