1
class MySet(set):
  def showme(self):
    print(self)
s1 = MySet([1, 2, 3])
s2 = MySet([2, 3, 4])
(s1&s2).showme()

should output:

{2, 3}

The problem is, that the operators return an instance of set, which lacks all of the stuff, that I added to it in my subclass. I know, I could override all methods, that retun "self" and wrap "self" into "MySet(self)". I want to void this, if possible and it would be ideal if this worked when using the operators on a MySet() & set()

Ingo
  • 75
  • 1
  • 9
  • 1
    Maybe a good compromise is https://stackoverflow.com/a/42491151/5760230, which programatically creates all the overloads. – Alex Jan 22 '21 at 09:51
  • Thanks, these are good pointers. I had hoped for something that's (if not in the language) in the base library. Like as a decorator or metaclass to apply to the newly created class. But this is a good start to create my own. – Ingo Jan 27 '21 at 10:43
  • After reading and thinking about the existing answers, I have created my own solution and posed it as an answer to this quesion: https://stackoverflow.com/questions/798442/what-is-the-correct-or-best-way-to-subclass-the-python-set-class-adding-a-new/65922199#65922199 – Ingo Jan 27 '21 at 15:26

0 Answers0