[Python-ideas] __data__

spir denis.spir at free.fr
Tue Feb 10 15:38:06 CET 2009

Le Wed, 11 Feb 2009 00:42:50 +1100,
Steven D'Aprano <steve at pearwood.info> a écrit :

> > Moreover --this is the reason why I first had to study that point
> > closer--, the present syntax requires the base type to be unique
> > *and* known at design time.   
> I don't think so. Just write a class factory.
>  >>> def factory(base):  
> ...     class MyThing(base):
> ...             def method(self):
> ...                     return "self is a %s" % base.__name__
> ...     return MyThing
> ...
>  >>> x = factory(int)()
>  >>> x.method()  
> 'self is a int'
>  >>> y = factory(str)()
>  >>> y.method()  
> 'self is a str'

I considered this approach already. It does not solve the problem at all. It's not the class's base type that is undefined at design time; it's the "base data"'s type. Each instance's base data may be of any type. In other words base==type(data) for each instance.
Researches on the topic have revealed nothing except for a thread on SWIG's site. They were confronted to precisely the same issue, and seemed to have no more clue on a possible workaroud.

la vida e estranya

More information about the Python-ideas mailing list