K
kj
Watch this:
<type 'dict'>
Bug? Feature? Genius beyond the grasp of schlubs like me?
~kj
<type 'dict'>
Bug? Feature? Genius beyond the grasp of schlubs like me?
~kj
copy, here, is a dict method. It will create a dict.Watch this:
<type 'dict'>
Bug? Feature? Genius beyond the grasp of schlubs like me?
Feature.
In (almost?) all cases any objects constructed by a subclass of a
builtin class will be of the original builtin class. So, for example,
subclass a string and concatenating your subclassed objects still
produces a string.
This is reasonable behaviour as for builtin classes performance is more
important than fully implementing polymorphism. If you want to subclass
a builtin class you need to be aware of this and override the behaviour
where it matters.
[...]I'd say it is neither, and call it a bloody nuisance that nevertheless
has some justification.
Yes, and the consequence is that any serious subclass must overload every
method which returns a new instance, otherwise your new subclass doesn't
"stick" -- you find it being replaced by the builtin as soon as you start
doing something useful with it.
This is especially a nuisance for subclasses of (say) float, where you
end up writing heaps of boilerplate like this:
class MyFloat(float):
def __add__(self, other):
return self.__class__(super(MyFloat, self).__add__(other))
# and the same for __mul__, __sub__, __rsub__, __pow__, ...
John O'Hagan said:IMO one of the benefits of subclassing is that you can just "bolt on"
additional behaviour without having to know all the inner workings of the
superclass, a benefit that is somewhat defeated by this behaviour of builtins.
In said:In (almost?) all cases any objects constructed by a subclass of a builtin
class will be of the original builtin class.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.