referring from within containers objects

Dimitris Garanatsios dg96057 at teledomenet.gr
Mon Mar 10 20:03:51 EST 2003


Terry Reedy wrote:
> "Dimitris Garanatsios" <dg96057 at teledomenet.gr> wrote in message
>>For your example i would use a class definition instead of a
> 
> dictionary like
> 
>>class Baptize:
>>     def __init__(self, fname, mname, lname):
>>         self.fname = fname
>>         self.mname = mname
>>         self.lname = lname
>>         self.fullname = fname +' '+ mname +' '+ lname
>>
>>and instantiate it like
>>
>>baby = Baptize('Pretty', 'Python', 'Syntax')
> 
> 
> A class is a good idea, but the above still has the same problem of
> consistency management.  One can use use __getattr__ and __setattr__
> to get and set virtual attributes as needed.  Even better: make
> Baptize a new class (subclass from object) and you can use a fullname
> property that will do the catenation on demand.  This is one of the
> sorts of things that properties were meant for ;-).
> 
> Terry J. Reedy
> 

I admit that new-style classes is a powerfull tool indeed but they seem 
to be under heavy development for the last few months... So far i have 
come into several problems using them because of the lurking bugs among 
their innocent implementation :-) that made my code unportable between 
the various 2.x.x python versions due to bugfixes that were released...

So until the whole situation is somewhat stabilized (and i imagine this 
should be soon) the godfather can better Baptize babies the old way like 
this:

class Baptize:
     def __init__(self, fname, mname, lname):
         self.fname = fname
         self.mname = mname
         self.lname = lname

     def fullName(self):
         return self.fname +' '+ self.mname +' '+ self.lname

while keeping data consistent at the same time and with no need to 
complicate things using __getattr__ and __setattr__.

Of course this is just an example to demonstrate an alternative to the 
original idea, afterall we all know that it whould be best to 
godfather.baptize(baby) using simple and more appropriate data 
structures ;-)





More information about the Python-list mailing list