steve at pearwood.info
Tue Feb 10 14:42:50 CET 2009
> I stepped on an issue recently that let me reconsider what we
> actually do and mean when subtyping, especially when deriving from
> built-in types. The issue basically is I need to add some information
> and behaviour to "base data" that can be of any type. (See PS for
> When subtyping int, I mean instances of MyInt to basically "act as"
> an int. This does not mean, in fact, inheriting int's attributes.
It doesn't? How bizarre.
> This means instead the integer specific operations to apply on a very
> special attribute instead of applying on the global object.
That sounds vaguely like delegation.
> 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)()
'self is a int'
>>> y = factory(str)()
'self is a str'
More information about the Python-ideas