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)