不可能重写 int 子类中的 int 值?
Rewriting over int value in int subclass impossible?
我目前在 的 python 3 项目中,我经常使用整数的二进制表示,因此,我制作了一个 classe 来完成这项工作对我自己来说更容易,但我不能让它完全发挥作用:
class EnhancedInt(int):
def __init__(self, x: Union[str, bytes, SupportsInt] = ...) -> None:
int.__init__(int(x))
def __getitem__(self, key: int) -> int:
"""Returns the digit number key from the binary representation of self (little endian)
Args:
key (int):
Returns:
int:
"""
if key > 32:
raise ValueError("int are 32 bits long, %d given is too much" % key)
if key < 0:
raise ValueError("Negative keys not supported, %d given is too low" % key)
else:
return EnhancedInt((self >> key) & 1)
def __setitem__(self, key: int, value: int) -> None:
if value != 0 and value != 1:
raise ValueError('Value must be 0 or 1')
self -= self[key]*pow(2, key)
if value:
self += pow(2, key)
因此,无效的部分:__setitem__
。我明白为什么,更改 self
似乎有点残酷,但我找不到值存储在 int 中的位置。
为了进一步理解,这里是调用我的 class 的代码:
>>> i = EnhancedInt(5)
>>> print(i[1])
0
>>> i[1] = 1
>>> print(i)
5 ????
我想返回 7 个,但目前只返回 5 个。
我找到了一个可行的解决方案,尽管与我想做的相比,它需要做很多工作。我做了以下操作:
class EnhancedInt(int):
"""
Does everything a int can do, but can also be addressed bitwise (you can read, write,
add and delete a bit at given position)
Bit representation is in little endian : the lowest indexes corresponding to the least
significant bits
"""
def __init__(self, x: Union[str, bytes, SupportsInt]) -> None:
int.__init__(int(x))
self.__value__ = int(x)
def __getitem__(self, key: int) -> int:
"""Returns the digit number *key* from the binary representation of *self* (little endian)
Args:
key (int): bit number to be returned
Returns:
int: value of the bit addressed
"""
EnhancedInt._check_key_value(key)
return (self.__value__ >> key) & 1
def __setitem__(self, key: int, value: int) -> None:
"""Changes the value of the *key*th bit of *self* (little endian)
Args:
key (int): index of bit to be modified
value (int): bit value (must be 0 or 1)
"""
EnhancedInt._check_key_value(key)
if value != 0 and value != 1:
raise ValueError("Value must be 0 or 1, %d given" % value)
if (not self[key]) and value:
self.__value__ += 1 << key
return None
if self[key] and not value:
self.__value__ -= 1 << key
我还重新定义了用于 class int 的所有方法。
这种方法似乎有点矫枉过正,而且还有其他缺陷。我想找到一种更优雅的方式,但与此同时,这会。
完整代码可以在以下地址找到:https://github.com/assombrance/Quantomatic/blob/master/src/data.py
我目前在 的 python 3 项目中,我经常使用整数的二进制表示,因此,我制作了一个 classe 来完成这项工作对我自己来说更容易,但我不能让它完全发挥作用:
class EnhancedInt(int):
def __init__(self, x: Union[str, bytes, SupportsInt] = ...) -> None:
int.__init__(int(x))
def __getitem__(self, key: int) -> int:
"""Returns the digit number key from the binary representation of self (little endian)
Args:
key (int):
Returns:
int:
"""
if key > 32:
raise ValueError("int are 32 bits long, %d given is too much" % key)
if key < 0:
raise ValueError("Negative keys not supported, %d given is too low" % key)
else:
return EnhancedInt((self >> key) & 1)
def __setitem__(self, key: int, value: int) -> None:
if value != 0 and value != 1:
raise ValueError('Value must be 0 or 1')
self -= self[key]*pow(2, key)
if value:
self += pow(2, key)
因此,无效的部分:__setitem__
。我明白为什么,更改 self
似乎有点残酷,但我找不到值存储在 int 中的位置。
为了进一步理解,这里是调用我的 class 的代码:
>>> i = EnhancedInt(5)
>>> print(i[1])
0
>>> i[1] = 1
>>> print(i)
5 ????
我想返回 7 个,但目前只返回 5 个。
我找到了一个可行的解决方案,尽管与我想做的相比,它需要做很多工作。我做了以下操作:
class EnhancedInt(int):
"""
Does everything a int can do, but can also be addressed bitwise (you can read, write,
add and delete a bit at given position)
Bit representation is in little endian : the lowest indexes corresponding to the least
significant bits
"""
def __init__(self, x: Union[str, bytes, SupportsInt]) -> None:
int.__init__(int(x))
self.__value__ = int(x)
def __getitem__(self, key: int) -> int:
"""Returns the digit number *key* from the binary representation of *self* (little endian)
Args:
key (int): bit number to be returned
Returns:
int: value of the bit addressed
"""
EnhancedInt._check_key_value(key)
return (self.__value__ >> key) & 1
def __setitem__(self, key: int, value: int) -> None:
"""Changes the value of the *key*th bit of *self* (little endian)
Args:
key (int): index of bit to be modified
value (int): bit value (must be 0 or 1)
"""
EnhancedInt._check_key_value(key)
if value != 0 and value != 1:
raise ValueError("Value must be 0 or 1, %d given" % value)
if (not self[key]) and value:
self.__value__ += 1 << key
return None
if self[key] and not value:
self.__value__ -= 1 << key
我还重新定义了用于 class int 的所有方法。 这种方法似乎有点矫枉过正,而且还有其他缺陷。我想找到一种更优雅的方式,但与此同时,这会。 完整代码可以在以下地址找到:https://github.com/assombrance/Quantomatic/blob/master/src/data.py