Problem subclassing tuple
Michele Simionato
mis6 at pitt.edu
Wed Apr 30 08:44:24 EDT 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.
>
> Skip
Indeed. According to Guido (see
http://www.python.org/2.2.2/descrintro.html#subclassing):
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
object
implementation to lift it. However, you can create mix-in classes by
inheriting from "object".
Here is an exapmple for the OP's edification:
class Holder(tuple):
def __new__(cls, *args, **kargs):
return super(Holder,cls).__new__(cls, args)
class Pretty(object):
def __str__(self):
return '*** %s ***' % super(Pretty,self).__str__()
class PrettyHolder(Pretty,Holder): pass
t=PrettyHolder(1,2)
print t #=> *** (1, 2) ***
More information about the Python-list
mailing list