是否可以创建一个整数类,其中某个类的实例(例如 AutomaticCounter
)每次被调用时都会增加某个数字(例如 1)?
>>> n = AutomaticCounter(start=0)
>>> print(n)
1
>>> print(n)
2
这是我迄今为止尝试过的:
class AutomaticCounter(int):
def __init__(self):
self = 0
def __str__(self):
self = self + 1
return self
最佳答案
如果你真的,真的,真的需要破坏一个不可变和内置的 类型,然后你可以创建一个指向它的“指针”:
class AutomaticCounter(int):
def __new__(cls, *args, **kwargs):
# create a new instance of int()
self = super().__new__(cls, *args, **kwargs)
# create a member "ptr" and storing a ref to the instance
self.ptr = self
# return the normal instance
return self
def __str__(self):
# first, create a copy via int()
# which "decays" from your subclass to an ordinary int()
# then stringify it to obtain the normal __str__() value
value = str(int(self.ptr))
# afterwards, store a new instance of your subclass
# that is incremented by 1
self.ptr = AutomaticCounter(self.ptr + 1)
return value
n = AutomaticCounter(0)
print(n) # 0
print(n) # 1
print(n) # 2
# to increment first and print later, use this __str__() instead:
def __str__(self):
self.ptr = AutomaticCounter(self.ptr + 1)
return str(int(self.ptr))
但是,这并不会使类型本身变得不可变。如果您在 __str__()
的开头执行 print(f"{self=}")
,您将看到实例没有更改,因此您实际上拥有的大小为您的对象的 2x int()
(+一些垃圾),您可以通过 self.ptr
访问真实实例。
它不能单独使用 self
,因为 self
只是传递给的只读引用(通过 __new__()
创建)实例的方法作为第一个参数,所以像这样:
def func(instance, ...):
instance = <something else>
正如 Daniel 所提到的,你做作业会,只需为名为 instance
( self
is just a quasi-standard name for the reference ) 的 local 变量分配一个新值,这并不会真正更改实例。因此,下一个看起来类似的解决方案是一个指针,当您想按照您描述的方式操作它时,我将它“隐藏”到一个名为 ptr
的自定义成员。
正如 user2357112 所指出的,由于实例不可变会导致不同步,因此如果您选择 self.ptr hack,则需要更新魔术方法 (__*__()
) >),例如,这是更新 __add__()
。请注意 int()
调用,它将其转换为 int()
以防止递归。
class AutomaticCounter(int):
def __new__(cls, *args, **kwargs):
self = super().__new__(cls, *args, **kwargs)
self.ptr = self
return self
def __str__(self):
value = int(self.ptr)
self.ptr = AutomaticCounter(int(self.ptr) + 1)
return str(value)
def __add__(self, other):
value = other
if hasattr(other, "ptr"):
value = int(other.ptr)
self.ptr = AutomaticCounter(int(self.ptr) + value)
return int(self.ptr)
def __rmul__(self, other):
# [1, 2, 3] * your_object
return other * int(self.ptr)
n = AutomaticCounter(0)
print(n) # 0
print(n) # 1
print(n) # 2
print(n+n) # 6
但是,任何尝试提取原始值或尝试使用 C API 访问它的操作很可能会失败,即反向操作,例如对于那些无法可靠地编辑魔术方法的人来说,使用不可变的内置函数应该是这种情况,因此它在所有模块和所有作用域中都得到了纠正。
示例:
# will work fine because it's your class
a <operator> b -> a.__operator__(b)
vs
# will break everything because it's using the raw value, not self.ptr hack
b <operator> a -> b.__operator__(a)
出于某种原因,list.__mul__()
除外。当我在 CPython 中找到代码行时,我会将其添加到此处。
或者,更明智的解决方案是创建一个自定义的可变对象,在其中创建一个成员并对其进行操作。然后返回它,字符串化,在
__str__
中:
class AutomaticCounter(int):
def __init__(self, start=0):
self.item = start
def __str__(self):
self.item += 1
return str(self.item)
关于python - 自动计数器作为整数的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70545606/