子类化int并重写uuu init_uuu方法-Python
可能重复:子类化int并重写uuu init_uuu方法-Python,python,int,subclassing,built-in,Python,Int,Subclassing,Built In,可能重复: 大家好 我试图对int类进行子类化,但没有成功。以下是我的尝试: class SpecialInt(int): def __init__(self, x, base=10, important_text=''): int.__init__(self, x, base) self.important_text=important_text 如果我执行以下操作: integer = SpecialInt(123, 10, 'rage of th
大家好 我试图对int类进行子类化,但没有成功。以下是我的尝试:
class SpecialInt(int):
def __init__(self, x, base=10, important_text=''):
int.__init__(self, x, base)
self.important_text=important_text
如果我执行以下操作:
integer = SpecialInt(123, 10, 'rage of the unicorns')
我得到这个错误:
TypeRror: int() takes at most 2 arguments (3 given)
有什么想法吗?:) 见:
\uuuu new\uuuu
()主要用于允许不可变类型的子类(如int、str或tuple)自定义实例创建。为了自定义类的创建,它通常在自定义元类中被重写
重复。看这里:看