0

While this works to create a subclass of list with only integers as items (simplified version, with only constructor overloading), it doesn't with tuples. Shouldn't super().__init__ there be calling tuple.__init__ when the decorated class is a subclass of tuple ?

#!/usr/bin/env python3

def only(thetype):
    def __(cls):
        class _(cls):
            def __init__(self,iterable):
                super().__init__( [ thetype(x) for x in iterable ] )
        return _
    return __

@only(int)
class myList(list):
    pass

l = myList( [1,2,3,3.14,"42"] )
print(l) # [1, 2, 3, 3, 42 ]

@only(int)
class myTuple(tuple):
    pass

t = myTuple( [1,2,3,3.14, "42"] )
# TypeError: object.__init__() takes no arguments
print(t)
khelwood
  • 55,782
  • 14
  • 81
  • 108
Python
  • 1
  • 1
    Have a look [here](https://stackoverflow.com/questions/1565374/subclassing-python-tuple-with-multiple-init-arguments) The question is slightly different, but the answer is the same. – Valentino Jan 05 '19 at 17:55
  • 1
    Possible duplicate of [Subclassing Python tuple with multiple \_\_init\_\_ arguments](https://stackoverflow.com/questions/1565374/subclassing-python-tuple-with-multiple-init-arguments) – hjpotter92 Jan 05 '19 at 17:56

0 Answers0