Slave to master auto linking.

Chris Angelico rosuav at gmail.com
Sun Nov 13 21:15:05 EST 2011


2011/11/14 Богун Дмитрий <vugluskr at vugluskr.org.ua>:
> m = master()
> s = m.slave()
> s.master is m
>

Can you simply have m.slave() pass a parameter to the slave's constructor?

class Master(object):
   class Slave(object):
       def __init__(self,master):
           self.master=master
           print 'Slave.__init__: self.master: ', self.master
   def slave(self):
       return self.Slave(self)

Alternatively, you don't have to nest the classes at all:

class Slave(object):
   def __init__(self,master):
      self.master=master
      print 'Slave.__init__: self.master: ', self.master

class Master(object):
   def slave(self):
       return Slave(self)

By passing 'self' to the Slave() constructor, I give the slave a
chance to keep a reference to its own master.

Chris Angelico



More information about the Python-list mailing list