[英]Is there a way to redefine a built-in class in python?
Is there a way that you can redefine built-in classes in python (eg int).有没有办法可以重新定义 python 中的内置类(例如 int)。 When I just do
class int
then the type is __main__.int
not int
, is there any way to define it without the __main__
?当我只是做
class int
那么类型是__main__.int
而不是int
,有没有办法在没有__main__
的情况下定义它?
i don't think thats possible without editing python source written in c but you can inherit from the built in classes like int
then add your methods like:我认为如果不编辑用 c 编写的 python 源代码,这是不可能的,但是您可以从
int
等内置类继承,然后添加您的方法,例如:
class MyList(list):
def sumOfAll(self):
return sum(self)
myList = MyList([1,2,3])
print(myList.sumOfAll())
Output: 6
Output:
6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.