将数字除以 Python 中我的 class 的实例
Dividing a number by instances of my class in Python
我有一个名为 Time
的 class,我需要实现一个 Frequency
class。如何实现将 int
s 或 float
s 除以 Time
的实例以获得 Frequency
的实例?
我已经知道 __div__
、__truediv__
、__floordiv__
和其他 Python 特殊方法,并且我已经在我的代码中使用它们来划分 classes 按数字或其他 classes 的实例,但我找不到将数字除以我的 class.
实例的方法
是否可以实现将数字除以 Python 中的 class 的实例?
您需要实施 __rtruediv__
和 __rfloordiv__
。
object.__radd__(self, other)
object.__rsub__(self, other)
object.__rmul__(self, other)
object.__rmatmul__(self, other)
object.__rtruediv__(self, other)
object.__rfloordiv__(self, other)
object.__rmod__(self, other)
object.__rdivmod__(self, other)
object.__rpow__(self, other)
object.__rlshift__(self, other)
object.__rrshift__(self, other)
object.__rand__(self, other)
object.__rxor__(self, other)
object.__ror__(self, other)
These methods are called to implement the binary arithmetic operations
(+, -, *, @, /, //, %, divmod(), pow(), **, <<, >>, &, ^, |) with
reflected (swapped) operands. These functions are only called if the
left operand does not support the corresponding operation [3] and the
operands are of different types. [4] For instance, to evaluate the
expression x - y, where y is an instance of a class that has an
__rsub__()
method, y.__rsub__(x)
is called if x.__sub__(y)
returns NotImplemented.
__rtruediv__
方法正是您要找的。
x / y
执行时,如果type(x)
没有实现__div__(self, other)
方法,其中other
可以是classtype(y)
,那么type(y).__rtruediv__(y, x)
被执行,返回结果。
用法:
class Foo:
def __init__(self, x):
self.x = x
def __truediv__(self, other):
return self.x / other
def __rtruediv__(self, other):
return other / self.x
>>> f = Foo(10)
>>> f / 10
1.0
>>> 10 / f
1.0
是的。您只需要确保 Time.__rtruediv__()
return 在收到浮点数或整数时是一个 Frequency
实例。
用法:
>>> 100 / Time(2)
Frequency(50.0)
>>> 2.5 / Time(5)
Frequency(0.5)
实施:
class Time:
def __init__(self, value):
self.value = value
def __rtruediv__(self, other):
if not isinstance(other, (int, float)):
return NotImplemented
return Frequency(other / self.value)
class Frequency:
def __init__(self, value):
self.value = value
def __repr__(self):
return '{}({})'.format(self.__class__.__name__, self.value)
python 文档包含有关 implementing the arithmetic operations 的完整示例,供您自定义 类。
处理不兼容类型的正确方法是 return 特殊值 NotImplemented
。
Special value which should be returned by the binary
special methods (e.g. __eq__()
, __lt__()
, __add__()
, __rsub__()
, etc.)
to indicate that the operation is not implemented with respect to the
other type
假设您尝试使用不受支持的复数,returning NotImplemented
最终会导致 TypeError
并显示正确的错误消息。 (至少在 python 3)
>>> 100j / Time(2)
Traceback (most recent call last):
File "python", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'complex' and 'Time'
我有一个名为 Time
的 class,我需要实现一个 Frequency
class。如何实现将 int
s 或 float
s 除以 Time
的实例以获得 Frequency
的实例?
我已经知道 __div__
、__truediv__
、__floordiv__
和其他 Python 特殊方法,并且我已经在我的代码中使用它们来划分 classes 按数字或其他 classes 的实例,但我找不到将数字除以我的 class.
是否可以实现将数字除以 Python 中的 class 的实例?
您需要实施 __rtruediv__
和 __rfloordiv__
。
object.__radd__(self, other)
object.__rsub__(self, other)
object.__rmul__(self, other)
object.__rmatmul__(self, other)
object.__rtruediv__(self, other)
object.__rfloordiv__(self, other)
object.__rmod__(self, other)
object.__rdivmod__(self, other)
object.__rpow__(self, other)
object.__rlshift__(self, other)
object.__rrshift__(self, other)
object.__rand__(self, other)
object.__rxor__(self, other)
object.__ror__(self, other)
These methods are called to implement the binary arithmetic operations (+, -, *, @, /, //, %, divmod(), pow(), **, <<, >>, &, ^, |) with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation [3] and the operands are of different types. [4] For instance, to evaluate the expression x - y, where y is an instance of a class that has an
__rsub__()
method,y.__rsub__(x)
is called ifx.__sub__(y)
returns NotImplemented.
__rtruediv__
方法正是您要找的。
x / y
执行时,如果type(x)
没有实现__div__(self, other)
方法,其中other
可以是classtype(y)
,那么type(y).__rtruediv__(y, x)
被执行,返回结果。
用法:
class Foo:
def __init__(self, x):
self.x = x
def __truediv__(self, other):
return self.x / other
def __rtruediv__(self, other):
return other / self.x
>>> f = Foo(10)
>>> f / 10
1.0
>>> 10 / f
1.0
是的。您只需要确保 Time.__rtruediv__()
return 在收到浮点数或整数时是一个 Frequency
实例。
用法:
>>> 100 / Time(2)
Frequency(50.0)
>>> 2.5 / Time(5)
Frequency(0.5)
实施:
class Time:
def __init__(self, value):
self.value = value
def __rtruediv__(self, other):
if not isinstance(other, (int, float)):
return NotImplemented
return Frequency(other / self.value)
class Frequency:
def __init__(self, value):
self.value = value
def __repr__(self):
return '{}({})'.format(self.__class__.__name__, self.value)
python 文档包含有关 implementing the arithmetic operations 的完整示例,供您自定义 类。
处理不兼容类型的正确方法是 return 特殊值 NotImplemented
。
Special value which should be returned by the binary special methods (e.g.
__eq__()
,__lt__()
,__add__()
,__rsub__()
, etc.) to indicate that the operation is not implemented with respect to the other type
假设您尝试使用不受支持的复数,returning NotImplemented
最终会导致 TypeError
并显示正确的错误消息。 (至少在 python 3)
>>> 100j / Time(2)
Traceback (most recent call last):
File "python", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'complex' and 'Time'