严格超载运营商。 [英] Overloading operators on the rigth.

查看:65
本文介绍了严格超载运营商。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之:

当重载比较运算符<,>时,等于__radd ___(等于右侧超载

+)的等价物, ==等等。我的

首先猜测__rlt__

重载<<没有工作。

扩展版本:

在一类数字中,我已经能够重载四个算术

运算符+, - , *和/通过在我的班级中定义方法__add__和__radd__

(etc ..)。这使我能够评估表达式

" instance_of_my_class +" instance_of_my_class" ,instance + non_instance

(带__add__)以及non_instance + instance(带__radd__)。

但我猜测定义方法__rlt__ in我的班级是为了能够评估表达式" non_instance<实例"不起作用。

那么比较运算符的名称(如果存在的话)是什么?

<,>,==等等,当一个想要将他们的权利方面交给

用户定义的(一个实例)类?


如果是直接电子邮件回复请删除法语翻译

我的签名中的nospam,也就是说字符串:

pasdepourriels。

解决方案

denis wendum< we ********** @ pasdepourriels.edf.fr>写道:

但我想在我的班级中定义方法__rlt__,以便能够评估表达式non_instance<实例"不工作。


明智的猜测,但不是事情的运作方式。

那么比较运营商的名称是什么(如果存在的话)
<,>,==等等,当一个人想要将他们的方法权限交给
用户定义的(一个实例)类时?




这里是如何找出:

class foo:
.... def __getattr __(self,name):

.... print''寻找%r''%name

....引发AttributeError,name

.... f = foo()
12< f
寻找''__gt__''

寻找''__coerce__''

寻找''__cmp__''

真的




看看这里发生了什么?在确定int(12的类型)

无法处理将12与foo的实例进行比较后,Python的< b $ b实现<转向右侧...并首先要求一个

__gt__方法!有道理,因为12< f可能会与f>相同。 12 ...一旦它找不到__gt__

实现继续尝试强制f到int(通过检查是否
类foo支持__coerce__)和最后通过尝试旧式

比较方法__cmp__。但是你可能不在乎;

而是,它的主旨是:写下你的__gt__ - 这也将被用作

作为你的__ rlt __ ; - 依此类推!

Alex


2004年10月12日星期二16:06:52 +0200, al ***** @ yahoo.com (Alex Martelli)写道:

denis wendum< we **********@pasdepourriels.edf.fr>写道:

但我想在我的班级中定义方法__rlt__,以便能够评估表达式non_instance<实例"没有用。



明智的猜测,但不是事情的运作方式。

那么名字是什么(如果它们存在的话)比较运算符
<,>,==等等,当一个人想要将他们的方法权限放在
用户定义的(一个实例)类时?



这里是如何找出:

class foo:... def __getattr __(self,name):
...打印''寻找%r''%名称
...引发AttributeError,名称
... f = foo()
12<为''__gt__'寻找'__coerce__''
寻找''__cmp__''
真的
看看这里发生了什么?在确定int(12的类型)无法处理与foo实例的比较12之后,Python的实现<转向右侧......首先要求一个
__gt__方法!有道理,因为12< f可能与f>相同。 12 ...一旦它找不到__gt__
实现继续尝试强制f到int(通过检查类foo是否支持__coerce__),最后通过尝试旧式<比较方法__cmp__。但是你可能不在乎;
相反,它的要点是:写下你的__gt__ - 它也将被用作你的__ rlt __ - 依此类推!




在我的系统上,(NT4,python 2.3.2)newstyle类真正发生了什么

isn'从那个实验中可以看出:

class foo(object):
... def __getattr __(self,name):

.. 。打印''寻找%r''%名称

...引发AttributeError,名称

... f = foo()
12< f
正确


旧样式的功能大致相同(但请注意默认的最终结果;-)

这是2.3.2 bug?

class foo:
... def __getattr __(self,name):

... print''正在寻找%r''%name

...引发AttributeError,名称

... f = foo()
12< f



寻找''__gt__''

寻找''__coerce__''

寻找''__cmp__''

错误


问候,

Bengt Richter


我认为,在寻找特殊方法时,__getattr__没有

有机会运行的新式课程的区别。

我确定这是记录在案的,但我没有看到它
http://python.org/2.2.2/descrintro.html

Jeff


----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.2.6(GNU / Linux)

iD8DBQFBbIUEJd01MZaTXX0RAgC4AJ9VDc64p8uTr + 2RqIjDqs / wjdcN4wCgiGBs

iyu6fjYUTutHsSOBvTFE1ds =

= Cnor

----- END PGP SIGNATURE -----

In a nutshell:
What is the equivalent of __radd__ (wich overloads the right hand side
of +) when overloading the comparison operators <,>,== and so on. My
first guess __rlt__ for
overloading the rigth hand side of < did not work.
Expanded version:
In a class of numbers I have been able to overload the four arithmetic
operators +,-,* and / by defining the methods __add__ and __radd__
(etc..) in my class. This enables me to evaluate expressions
"instance_of_my_class+"instance_of_my_class" ,"instance + non_instance"
(with __add__) as well as "non_instance+instance"(with __radd__).
But my guess to define the method __rlt__ in my class in order to be
able to evaluate expressions "non_instance < instance" did not work.
So what are the names (if they exist at all) of the comparison operators
<,>,== and so on, when one wants to exend their rigth hand side to a
user defined (instance of a) class?

In case of direct e-mail response please remove the french translation
of nospam in my signature, that is to say the string :
pasdepourriels.

解决方案

denis wendum <we**********@pasdepourriels.edf.fr> wrote:

But my guess to define the method __rlt__ in my class in order to be
able to evaluate expressions "non_instance < instance" did not work.
Sensible guess, but not the way things work.
So what are the names (if they exist at all) of the comparison operators
<,>,== and so on, when one wants to exend their rigth hand side to a
user defined (instance of a) class?



Here''s how to find out:

class foo: .... def __getattr__(self, name):
.... print ''looking for %r'' % name
.... raise AttributeError, name
.... f=foo()
12 < f looking for ''__gt__''
looking for ''__coerce__''
looking for ''__cmp__''
True



See what''s happening here? After determining that int (the type of 12)
cannot deal with comparing 12 with an instance of foo, Python''s
implementation of < turns to the right hand side... and asks for a
__gt__ method first! Makes sense, because 12 < f is presumably going to
be the same thing as f > 12 ... once it doesn''t find __gt__ the
implementation continues by trying to coerce f to an int (by checking if
class foo supports __coerce__) and lastly by trying for the old-style
comparison method __cmp__. But you presumably don''t care about that;
rather, the gist of it is: write your __gt__ -- that will also be used
as your "__rlt__" -- and so on!
Alex


On Tue, 12 Oct 2004 16:06:52 +0200, al*****@yahoo.com (Alex Martelli) wrote:

denis wendum <we**********@pasdepourriels.edf.fr> wrote:

But my guess to define the method __rlt__ in my class in order to be
able to evaluate expressions "non_instance < instance" did not work.



Sensible guess, but not the way things work.

So what are the names (if they exist at all) of the comparison operators
<,>,== and so on, when one wants to exend their rigth hand side to a
user defined (instance of a) class?



Here''s how to find out:

class foo:... def __getattr__(self, name):
... print ''looking for %r'' % name
... raise AttributeError, name
... f=foo()
12 < flooking for ''__gt__''
looking for ''__coerce__''
looking for ''__cmp__''
True
See what''s happening here? After determining that int (the type of 12)
cannot deal with comparing 12 with an instance of foo, Python''s
implementation of < turns to the right hand side... and asks for a
__gt__ method first! Makes sense, because 12 < f is presumably going to
be the same thing as f > 12 ... once it doesn''t find __gt__ the
implementation continues by trying to coerce f to an int (by checking if
class foo supports __coerce__) and lastly by trying for the old-style
comparison method __cmp__. But you presumably don''t care about that;
rather, the gist of it is: write your __gt__ -- that will also be used
as your "__rlt__" -- and so on!



On my system, (NT4, python 2.3.2) what really happens for newstyle classes
isn''t apparent from that experiment:

class foo(object): ... def __getattr__(self, name):
... print ''looking for %r'' % name
... raise AttributeError, name
... f=foo()
12 < f True

The old style does work much the same (but note the default end result ;-)
Is that a 2.3.2 bug?
class foo: ... def __getattr__(self, name):
... print ''looking for %r'' % name
... raise AttributeError, name
... f=foo()
12 < f


looking for ''__gt__''
looking for ''__coerce__''
looking for ''__cmp__''
False

Regards,
Bengt Richter


I think it''s a difference in new-style classes that __getattr__ doesn''t
have a chance to run when searching for special methods.

I''m sure this is documented, but I didn''t see it in
http://python.org/2.2.2/descrintro.html

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFBbIUEJd01MZaTXX0RAgC4AJ9VDc64p8uTr+2RqIjDqs/wjdcN4wCgiGBs
iyu6fjYUTutHsSOBvTFE1ds=
=Cnor
-----END PGP SIGNATURE-----


这篇关于严格超载运营商。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆