Python assert isinstance() 向量

Ste*_*n B 0 python assert duck-typing isinstance

我正在尝试在 python 中实现一个 Vector3 类。如果我用 c++ 或 c# 编写 Vector3 类,我会将 X、Y 和 Z 成员存储为浮点数,但在 python 中我读到ducktyping 是要走的路。所以根据我的 c++/c# 知识,我写了这样的东西:

class Vector3:
    def __init__(self, x=0.0, y=0.0, z=0.0):
        assert (isinstance(x, float) or isinstance(x, int)) and (isinstance(y, float) or isinstance(y, int)) and \
               (isinstance(z, float) or isinstance(z, int))
        self.x = float(x)
        self.y = float(y)
        self.z = float(z)
Run Code Online (Sandbox Code Playgroud)

问题是关于断言语句:在这种情况下你会使用它们还是不使用它们(数学的 Vector3 实现)。我也用它来做类似的操作

def __add__(self, other):
    assert isinstance(other, Vector3)
    return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
Run Code Online (Sandbox Code Playgroud)

你会在这些情况下使用断言吗?根据这个网站:https : //wiki.python.org/moin/UsingAssertionsEffectively它不应该被过度使用,但对于我这个一直使用静态类型的人来说,不检查相同的数据类型是非常奇怪的。

Mos*_*oye 5

assert比在生产代码中闲逛更适合用于调试。您可以改为创建属性为载体的属性xy并且z,和raise ValueError当传递的值是所需的类型不是:

class Vector3:
    def __init__(self, x=0.0, y=0.0, z=0.0):
        self.x = x
        self.y = y
        self.z = z

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, val):
        if not isinstance(val, (int, float)):
            raise TypeError('Inappropriate type: {} for x whereas a float \
            or int is expected'.format(type(val)))
        self._x = float(val)

    ...
Run Code Online (Sandbox Code Playgroud)

注意 howisinstance也需要一个类型的元组。

__add__操作符中,您raise TypeError还需要包含一条适当的消息:

def __add__(self, other):
    if not isinstance(other, Vector3):
        raise TypeError('Object of type Vector3 expected, \
        however type {} was passed'.format(type(other)))
    ...
Run Code Online (Sandbox Code Playgroud)