object vs class oriented -- xotcl

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jan 29 18:06:23 EST 2008


En Tue, 29 Jan 2008 16:22:24 -0200, William Pursell  
<bill.pursell at gmail.com> escribi�:

> I'm fairly excited at the idea of being able to
> do per-object mixins in xotcl.  I guess it would
> look like this in python:
>
> BROKEN CODE:
> a = object()
> a.__class__.append( foo )
> a.__class__.append( bar )
>
> In python, if you want an object to me a member
> of 2 classes, it seems that you have no choice but
> to declare a new class that inherits from both.  eg:
>
> class foobar( foo, bar):
>   pass
> a = foobar()
>
> Is it possible to make an object be a member
> of 2 classes without defining such a class?  I
> believe "per object mixin" is the correct
> term for such an animal.  The first several google
> hits on that phrase all reference xotcl, so I'm
> not sure if that is an xotcl inspired vocabulary
> that isn't really standard.

No, an instance can be of only one class at a time. But you can generate  
dynamically the foobar class:
foobar = type("foobar", (foo,bar), {})
and provided `a` is an instance of any other user defined class (not bare  
object), this works:
a.__class__ = foobar
Or simply a = foobar()

If you are going to use this heavily, I suggest a "class cache" in order  
to avoid creating as many classes.

-- 
Gabriel Genellina




More information about the Python-list mailing list