相关疑难解决方法(0)

Python 3 __getattr__的行为与Python 2不同?

我需要编写一个实现32位无符号整数的类,就像它们在C编程语言中一样.我最关心的是二元转换,但我通常希望我的班级:

  1. 具有相同的接口,int具有与工作int正常
  2. 我的U32类(int + U32,U32+ int等)的任何操作也返回U32
  3. 是纯python - 我不想使用NumPy,ctypes等.

正如在这个答案中可以找到的,我得到了一个在Python 2下运行的解决方案.最近我尝试在Python 3下运行它并注意到虽然以下测试代码在旧版本的Python下工作正常,但Python 3引发了一个错误:

class U32:
    """Emulates 32-bit unsigned int known from C programming language."""

    def __init__(self, num=0, base=None):
        """Creates the U32 object.

        Args:
            num: the integer/string to use as the initial state
            base: the base of the integer use if the num given was a string
        """
        if base is None:
            self.int_ = int(num) % 2**32
        else:
            self.int_ = …
Run Code Online (Sandbox Code Playgroud)

python

5
推荐指数
1
解决办法
2610
查看次数

标签 统计

python ×1