Problem subclassing tuple
mis6 at pitt.edu
Wed Apr 30 14:44:24 CEST 2003
Skip Montanaro <skip at pobox.com> wrote in message news:<mailman.1051645702.13393.python-list at python.org>...
> >> You might want to make it "cooperative", that is, instead of using
> >> "tuple.__new__", use "super(Holder,cls).__new__"
> >> This delegates the search for the superclass to the function super();
> >> and that gives you the ability to have multiple inheritance, since
> >> super() will "do the right thing" when getting your current class'
> >> superclass; so every class which defines a __new__ method has it
> >> called.
> I don't believe it matters here. As I recall, builtins don't support
> multiple inheritance very well (if at all). The most you can do is add
> mixin classes.
Indeed. According to Guido (see
You can use multiple inheritance, but you can't multiply inherit from
different built-in types (for example, you can't create a type that
inherits from both the built-indict and list types). This is a
permanent restriction; it would require too many changes to Python's
implementation to lift it. However, you can create mix-in classes by
inheriting from "object".
Here is an exapmple for the OP's edification:
def __new__(cls, *args, **kargs):
return super(Holder,cls).__new__(cls, args)
return '*** %s ***' % super(Pretty,self).__str__()
class PrettyHolder(Pretty,Holder): pass
print t #=> *** (1, 2) ***
More information about the Python-list