[英]Python object class methods not inheriting
In Python operator overloading is achieved by defining special method in the class. 在Python中,运算符重载是通过在类中定义特殊方法来实现的。 For example, to use +
operator, define __add__
method. 例如,要使用+
运算符,请定义__add__
方法。 There are many other special methods for different operators. 对于不同的运算符,还有许多其他特殊方法。 In addition to that, all these special operators comes from object
class which is the base class for every other class. 除此之外,所有这些特殊运算符都来自object
类,而object
类是其他所有类的基类。 That means every class inherits special method like __add__
, __ge__
, __le__
and so on. 这意味着每个类都继承诸如__add__
, __ge__
, __le__
等的特殊方法。
Now Consider the following code. 现在考虑以下代码。
class test_class:
def ring(self):
print("ring")
obj = test_class()
print(obj.__ge__)
Output: 输出:
<method-wrapper '__ge__' of test_class object at 0x0000000002247EF0>
But when I try to access __add__
using obj
object, it gives me following error: 但是,当我尝试使用obj
对象访问__add__
,出现以下错误:
AttributeError: 'test_class' object has no attribute '__add__'
Why is that ? 这是为什么 ? Does that mean __add__
method doesn't inherit from the object
class ? 这是否意味着__add__
方法没有从object
类继承? What's exactly going on ? 到底是怎么回事?
The __add__
function is not an attribute of object
. __add__
函数不是object
的属性。 It is a function found in the operator module. 这是在操作员模块中找到的功能。 You can provide your own implementation of this by implementing your own version of these methods in your class. 您可以通过在类中实现这些方法的自己的版本来提供自己的实现。
This is also explained in the documentation here . 此处的文档中也对此进行了说明。
Observe: 注意:
>>> class Foo:
... def __add__(self, x):
... print("I took over the add with {}".format(x))
...
>>>
>>>
>>> f = Foo()
>>> f + 100
I took over the add with 100
>>>
Furthermore, even if you look at what is provided to you from object
, you will see: 此外,即使您查看object
提供给您的内容,您也会看到:
>>> dir(object)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>>
There is no implementation of the __add__
you are looking to override. 您要覆盖的__add__
没有实现。 These operators are overridable by declaring them yourself in your class. 通过在班级中声明自己,可以重写这些运算符。
To provide some context around why operators like __eq__
(and other comparison operators) operator might be implemented as part of object and not __add__
as an example, is because, per the documentation as well ( here ), these equality operators are rich-comparison methods and they provide their own implementation in the base object. 为了提供围绕为什么像运营商一些背景__eq__
(和其他比较符)运算符可以实现为对象的一部分,而不是__add__
作为一个例子,是因为,每个文档,以及( 在这里 ),这些等式操作符是富比较方法并且它们在基础对象中提供了自己的实现。 If you take a look at the implementation of the object
class, you will see a docstring mention of what will be returned when you actually use these operators with instance objects: 如果您看一下object
类的实现,您将看到一个文档字符串提到当您将这些运算符与实例对象实际一起使用时将返回什么:
def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass
As an example, you can see it mentions that when you try to perform a ">" with instances that inherit from object, they will use that implementation. 例如,您可以看到它提到当您尝试对从对象继承的实例执行“>”时,它们将使用该实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.