When subclassing builtin types, I noticed a rather important difference between Python 2 and Python 3 in the return type of the methods of the built-in types. The following code illustrates this for sets:
class MySet(set):
pass
s1 = MySet([1, 2, 3, 4, 5])
s2 = MySet([1, 2, 3, 6, 7])
print(type(s1.union(s2)))
print(type(s1.intersection(s2)))
print(type(s1.difference(s2)))
With Python 2, all the return values are of type MySet
. With Python 3, the return types are set
. I could not find any documentation on what the result is supposed to be, nor any documentation about the change in Python 3.
Anyway, what I really care about is this: is there a simple way in Python 3 to get the behavior seen in Python 2, without redefining every single method of the built-in types?
This isn't a general change for built-in types when moving from Python 2.x to 3.x --
list
andint
, for example, have the same behaviour in 2.x and 3.x. Only the set type was changed to bring it in line with the other types, as discussed in this bug tracker issue.I'm afraid there is no really nice way to make it behave the old way. Here is some code I was able to come up with:
This will simply overwrite all methods with versions that return your own type. (There is a whole lot of methods!)
Maybe for your application, you can get away with overwriting
copy()
and stick to the in-place methods.