Properties transfer between instances of different classes, without subclassing

Pierre pgmdevlist at mailcan.com
Sat Dec 10 02:26:08 EST 2005


Folks,
I'm pretty new to OOP, so I still have problems with inheritance and
delegation.
I defined 2 Numeric MaskedArray subclasses, as:

class Temp(MA,object):
   def __init__(self,data):
       self = MA.__init__(self,data)
   cold = property(fget lambda self:masked_where(self<10,self)
   warm = property(fget lambda self:masked_where(self>20,self)

class Tabl(MA,object):
    def __init__(self,vector)
        self.vector=vector
        tabl = apply_processing_functions_to(vector)
        self = MA.__init__(self,tabl)

where 'data' and 'vector' are themselves masked arrays.

I'd like to add the properties of the argument of an instance of Tabl
to this instance, only if the properties are not already defined.
For example, I create an instance of 'Temp' (say, 'tempv'), and
instance of 'Tabl' (say 'tablv'), this latter initialized with the
former.
v = arange(5,26)
tempv = Temp(v)
tabv = tabulated(tempv)

Ideally, 'tabv.cold' would give me 'tabv', masked where the values are
<10.
I don't want to make 'Tabl' a subclass of 'Temp'. I'd like to use it
more generically, so that when I define a 3rd class 'Color', I could
initiate 'Tabv' with an instance of 'Color', accessing the 'Color'
properties without the 'Temp' properties.

In short, I want to pass the properties of an instance attribute to the
instance...

Any ideas/comments will be welcome




More information about the Python-list mailing list