Properties transfer between instances of different classes, without subclassing

2005-12-09 Thread Pierre
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Properties transfer between instances of different classes, without subclassing

2005-12-10 Thread Heiko Wundram
Pierre wrote:
> 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.

I guess interfaces could make your day:

http://www.zope.org/Products/ZopeInterface

--- Heiko.
-- 
http://mail.python.org/mailman/listinfo/python-list