Dynamically determine base classes on instantiation
Thomas Bach
thbach at students.uni-mainz.de
Thu Aug 16 13:27:34 EDT 2012
On Thu, Aug 16, 2012 at 10:03:51AM -0700, Richard Thomas wrote:
> class Foo(object):
> def __new__(cls, arg):
> if isinstance(arg, list):
> cls = FooList
> elif isinstance(arg, dict):
> cls = FooDict
> return object.__new__(cls, arg)
>
> class FooList(Foo, list):
> pass
>
> class FooDict(Foo, dict):
> pass
>
> You could even have __new__ make these Foo* classes dynamically when
> it encounters a new type of argument.
>
> Chard.
Thanks for that one. Your solution just hit me like a punch in the
face. I had something similar in my mind. But I could not work out how
the mechanics behind it are working.
Regards,
Thomas
More information about the Python-list
mailing list