object inheritance

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Oct 31 05:57:41 CET 2007

En Wed, 31 Oct 2007 00:36:34 -0300, Anand <anandology at gmail.com> escribió:

>> No, that is an argument for multiple-inheritance, mixin classes etc. You
>> know when constructing the object what behaviour you want it to have. It
>> isn't an argument for changing the behaviour of an existing object
>> dynamically.

> Partially true. I don't want to change the behavior of an exiting
> object. I know what behavior I want to have at the time of creating.
> This can be achieved by creating classes dynamically by parameterizing
> the base class, which I thought is ugly.

If you know at compile time which features you want, you can use multiple  
inheritance to define the new class.
If you only know the set of clases to mix at run time, you can dinamically  
create new classes with 'type':

newclass = type('newclass', (CachedDB, SimpleDB), {})
db = newclass()
db.withIDs will call CachedDB.withID, as you want.

[In another message]
> In the example that I gave previously, call to self.say in A.foo
> calls B.say not A.say, which I think is not possible with delegation.

It's easy to create a new type inheriting from your classes:

obj = type('', (B,A), {})()
should print: b.say "foo"

Gabriel Genellina

More information about the Python-list mailing list