Issue 11854: or et al instantiate subclass of set without calling init (original) (raw)
If you create a subclass of set but do not override or, and, xor, and sub, calling these functions will yield a new instance of your subclass. The new instance will never have init called on it. Depending on what you expect init to do, this can cause problems later on.
The simplest solution would be to make these functions work like list.add. If you have two instances of some list subclass (foo and bar), type(foo.add(bar)) will just be list.
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
class SetSub(set): ... def init(self, *args, **kwargs): ... print 'foo' ... set.init(self, *args, **kwargs) ... a = SetSub([2,7]) foo b = set([1,7]) a ^ b SetSub([1, 2])
Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit (AMD64)] on win 32 Type "help", "copyright", "credits" or "license" for more information.
class SetSub(set): ... def init(self, *args, **kwargs): ... print('foo') ... set.init(self, *args, **kwargs) ... a = SetSub([2,7]) foo b = set([1,7]) a ^ b {1, 2}
2.6 is closed for anything except security patches at this point.
2.7 is only open for flat-out bugs, not API changes. So, we might be able to add code that could call the init method (if it exists) whenever a new set is created, but we couldn't change whether or returns a new set or frozenset instead of one of its subtypes. Existing, working code may reasonably rely on the current behavior of returning the subtype (FWIW, this also matches what was done in the original sets.py).