尽管这可以创建仅将整数作为项目的list子类(简化版本,仅具有构造函数重载),但它不包含元组。装饰的类是元组的子类时,super().__init__
是否应该调用tuple.__init__
?
#!/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)