相关疑难解决方法(0)

从str或int继承

为什么我在创建一个继承自str(或者也来自int)的类时遇到问题

class C(str):
   def __init__(self, a, b):
     str.__init__(self,a)
     self.b = b

C("a", "B")

TypeError: str() takes at most 1 argument (2 given)
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用int而不是str,它会发生同样的情况,但它适用于自定义类.我需要用__new__而不是__init__?为什么?

python string int inheritance new-operator

47
推荐指数
5
解决办法
2万
查看次数

标签 统计

inheritance ×1

int ×1

new-operator ×1

python ×1

string ×1