[New-bugs-announce] [issue15879] set.__or__, __and__, etc create subclass types, but ignore __new__

Jon Obermark report at bugs.python.org
Fri Sep 7 17:44:41 CEST 2012

New submission from Jon Obermark:

If they are not going to call the __metaclass__ or the class __new__, then they should return `set` objects instead of subclass objects, so that it is clear what is going on.

As it is, the results of set operations receive some subclass information but not all.  So they are not really obeying the notion of a subclass: the results are neither `set` objects, nor properly-constructed objects of the `set` subclass.

class Fooset(Set):
  def __new__(cls, s = []):
    print 'New called'
    self = super(Fooset, cls).__new__(cls)
    if isinstance(s, Fooset):
      self.foo = s.foo
      self.foo = 'default'
    return self

x = Fooset([1,2,5])
y = x|x

The object `y` reports being of the type `Fooset`, but has not been constructed by the `type` that makes `Fooset` objects.

messages: 169986
nosy: Jon.Obermark
priority: normal
severity: normal
status: open
title: set.__or__, __and__, etc create subclass types, but ignore __new__

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list