alias for data member of class instance?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Feb 5 16:50:08 EST 2007


Sean McIlroy a écrit :
> hi all
> 
> is there a way to do this ...
> 
> class clown:
> 	def __init__(self):
> 		self.x = 0
> 		self.y = ALIAS(self.x) ## FEASIBLE ?

class Alias(object):
    def __init__(self, attrname):
      self._attrname = attrname

    def __get__(self, instance, cls):
      if instance is None:
        return self
      return getattr(instance, self._attrname)

    def __set__(self, instance, value):
      setattr(instance, self._attrname, value)

class Clown2(object):
   def __init__(self):
     self.x = 0

   y = Alias('x')






More information about the Python-list mailing list