Boilerplate采用丰富的比较方法 [英] Boilerplate in rich comparison methods

查看:65
本文介绍了Boilerplate采用丰富的比较方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个实现丰富比较的课程,我发现自己

写了很多非常相似的代码。如果计算很简单并且

简单,我会这样做:

class Parrot:

def __eq __(self,other):

返回self.plumage()== other.plumage()

def __ne __(self,other):

返回self.plumage() != other.plumage()

def __lt __(self,other):

返回self.plumage()< other.plumage()

def __gt __(self,other):

返回self.plumage()other.plumage()

def __le__ (自我,其他):

返回self.plumage()< = other.plumage()

def __ge __(自我,其他):

返回self.plumage()> = other.plumage()

如果比较需要大量工作,我会做这样的事情:


级Aardvark:

def __le __(自我,其他):

返回lots_of_work(自我,其他)

def __gt __(自我,其他):

返回不自我< =其他

#等


但我可以'不要觉得有更好的方法。别人做了什么?


-

史蒂文。

I''m writing a class that implements rich comparisons, and I find myself
writing a lot of very similar code. If the calculation is short and
simple, I do something like this:
class Parrot:
def __eq__(self, other):
return self.plumage() == other.plumage()
def __ne__(self, other):
return self.plumage() != other.plumage()
def __lt__(self, other):
return self.plumage() < other.plumage()
def __gt__(self, other):
return self.plumage() other.plumage()
def __le__(self, other):
return self.plumage() <= other.plumage()
def __ge__(self, other):
return self.plumage() >= other.plumage()

If the comparison requires a lot of work, I''ll do something like this:

class Aardvark:
def __le__(self, other):
return lots_of_work(self, other)
def __gt__(self, other):
return not self <= other
# etc.

But I can''t help feeling that there is a better way. What do others do?

--
Steven.

推荐答案

Steven D''Aprano< st *** @ REMOVE.THIS.cybersource.com.auwrites:
Steven D''Aprano <st***@REMOVE.THIS.cybersource.com.auwrites:

class Parrot:

def __eq __(self,other):

返回self.plumage()== other.plumage()

def __ne __(self,other):

返回self.plumage()!= other.plumage()

def __lt __(self,other):

返回self.plumage()< ; other.plumage()

def __gt __(self,other):

返回self.plumage()other.plumage()

def __le__ (自我,其他):

返回self.plumage()< = other.plumage()

def __ge __(自我,其他):

返回self.plumage()> = other.plumage()
class Parrot:
def __eq__(self, other):
return self.plumage() == other.plumage()
def __ne__(self, other):
return self.plumage() != other.plumage()
def __lt__(self, other):
return self.plumage() < other.plumage()
def __gt__(self, other):
return self.plumage() other.plumage()
def __le__(self, other):
return self.plumage() <= other.plumage()
def __ge__(self, other):
return self.plumage() >= other.plumage()



如果它是统一的我认为你可以使用__cmp __:


类鹦鹉:

def __cmp __(自我,其他):

返回cmp(self.plumage(),other.plumage() )


我错过了什么吗?丰富比较的想法是,

不同的关系彼此之间并不那么相似,例如:某种

部分订购。

If it''s that uniform I think you can just use __cmp__:

class Parrot:
def __cmp__(self, other):
return cmp(self.plumage(), other.plumage())

Did I miss something? The idea of rich comparison is that the
different relations aren''t so similar to each other, e.g. some kind of
partial ordering.


如果比较需要大量的工作,我会做这样的事情:


级Aardvark:

def __le __(自我,其他):

返回lots_of_work(自我,其他)

def __gt __(自我,其他):

返回不自我< =其他

#等


但是我我不能不觉得有更好的方法。其他人做什么?
If the comparison requires a lot of work, I''ll do something like this:

class Aardvark:
def __le__(self, other):
return lots_of_work(self, other)
def __gt__(self, other):
return not self <= other
# etc.

But I can''t help feeling that there is a better way. What do others do?



与上面相同。

Same as above.


Steven D''Aprano写道:
Steven D''Aprano wrote:

我正在编写一个实现丰富比较的类,我发现自己

编写了很多非常相似的代码。如果计算很简单,那么我就做这样的事情:


class Parrot:

def __eq __(self,其他):

返回self.plumage()== other.plumage()

def __ne __(自我,其他):

返回自我.plumage()!= other.plumage()

def __lt __(self,other):

返回self.plumage()< other.plumage()

def __gt __(self,other):

返回self.plumage()other.plumage()

def __le__ (自我,其他):

返回self.plumage()< = other.plumage()

def __ge __(自我,其他):

返回self.plumage()> = other.plumage()

如果比较需要大量工作,我会做这样的事情:


级Aardvark:

def __le __(自我,其他):

返回lots_of_work(自我,其他)

def __gt __(自我,其他):

返回不自我< =其他

#等


但我可以'不要觉得有更好的方法。其他人做什么?
I''m writing a class that implements rich comparisons, and I find myself
writing a lot of very similar code. If the calculation is short and
simple, I do something like this:
class Parrot:
def __eq__(self, other):
return self.plumage() == other.plumage()
def __ne__(self, other):
return self.plumage() != other.plumage()
def __lt__(self, other):
return self.plumage() < other.plumage()
def __gt__(self, other):
return self.plumage() other.plumage()
def __le__(self, other):
return self.plumage() <= other.plumage()
def __ge__(self, other):
return self.plumage() >= other.plumage()

If the comparison requires a lot of work, I''ll do something like this:

class Aardvark:
def __le__(self, other):
return lots_of_work(self, other)
def __gt__(self, other):
return not self <= other
# etc.

But I can''t help feeling that there is a better way. What do others do?



曾几何时,我曾写过一个元类来生成boilerate,

填补空白。它不会对你必须实现的比较强加任何限制,例如你可以实现__le__和

__eq__,或__gt__和__ne__等。尝试品尝。

http://rafb.net/p/ mpvsIQ37.nln.html

George

Once upon a time I had written a metaclass to generate the boilerate,
filling in the gaps. It doesn''t impose any constraints on which
comparisons you must implement, e.g you may implement __le__ and
__eq__, or __gt__ and __ne__, etc. Season to taste.

http://rafb.net/p/mpvsIQ37.nln.html

George


" George Sakkis" < ge *********** @ gmail.comwrote in message

news:11 ******************* ***@m58g2000cwm.googlegr oups.com ...
"George Sakkis" <ge***********@gmail.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...

Steven D''Aprano写道:
Steven D''Aprano wrote:

>我正在写一个实现丰富比较的课程,我发现自己编写了很多非常相似的代码。如果计算简短且简单,我会这样做:

class Parrot:
def __eq __(self,other):
return self.plumage( )== other.plumage()
>I''m writing a class that implements rich comparisons, and I find myself
writing a lot of very similar code. If the calculation is short and
simple, I do something like this:
class Parrot:
def __eq__(self, other):
return self.plumage() == other.plumage()



< snip>

<snip>


>如果比较需要大量工作,我会做这样的事情:

def __le __(自我,其他):
返回lots_of_work(自我,其他)
def __gt __(自我,其他):
返回不自我< =其他
>If the comparison requires a lot of work, I''ll do something like this:

class Aardvark:
def __le__(self, other):
return lots_of_work(self, other)
def __gt__(self, other):
return not self <= other



< snip>

<snip>


曾几何时,我曾写过一个元类来生成boilerate,

填补空白。它不会对你必须实现的比较强加任何限制,例如你可以实现__le__和

__eq__,或__gt__和__ne__等。尝试品尝。

http://rafb.net/p/ mpvsIQ37.nln.html


George
Once upon a time I had written a metaclass to generate the boilerate,
filling in the gaps. It doesn''t impose any constraints on which
comparisons you must implement, e.g you may implement __le__ and
__eq__, or __gt__ and __ne__, etc. Season to taste.

http://rafb.net/p/mpvsIQ37.nln.html

George



关于编写这些比较运算符的附注。我记得当学习Java的时候,这真的是我第一次花这么多时间阅读关于身份测试与测试平等的内容。当时的Java

常规做法是通过测试来查看对象是否与自身进行比较,并且如果
/>
所以,切换到追逐并返回True(和不相等的相反

比较)。这背后的想法是,在代码中表面上有很多次

,其中一个对象与自身进行比较(而不是在一个

显式中如果x == x但是在隐式测试中,例如列表搜索和

过滤),这个前期测试身份,非常快,可能会b / b
短路,否则不必要比较。


在Python中,这看起来像:


class Parrot:

def __eq __(self,其他):

返回self是other或self.plumage()== other.plumage()

def __ne __(self,other):

返回self不是其他和self.plumage()!= other.plumage()

def __lt __(自我,其他):

返回self不是其他的self.plumage()< other.plumage()

def __gt __(自我,其他):

返回self不是其他和self.plumage()other.plumage()

def __le __(自我,其他):

返回self不是其他和self.plumage()< = other.plumage()

def __ge __(自我,其他):

返回self不是其他和self.plumage()> = other.plumage()


和George'的元类会有类似的变化。


另一方面,我没有在任何Python代码中看到这个成语,我已经阅读了b
想知道这是否只是当时的编码时尚。


不过,在史蒂文的Aardark课程中,可能值得绕过

如果你先测试看自己是不是
其他就叫做lot_of_work的东西。


- Paul

Just a side note on writing these comparison operators. I remember when
learning Java that this was really the first time I spent so much time
reading about testing-for-identity vs. testing-for-equality. The Java
conventional practice at the time was to begin each test-for-equality method
by testing to see if an object were being compared against itself, and if
so, cut to the chase and return True (and the converse for an inequality
comparison). The idea behind this was that there were ostensibly many times
in code where an object was being compared against itself (not so much in an
explicit "if x==x" but in implicit tests such as list searching and
filtering), and this upfront test-for-identity, being very fast, could
short-circuit an otherwise needless comparison.

In Python, this would look like:

class Parrot:
def __eq__(self, other):
return self is other or self.plumage() == other.plumage()
def __ne__(self, other):
return self is not other and self.plumage() != other.plumage()
def __lt__(self, other):
return self is not other and self.plumage() < other.plumage()
def __gt__(self, other):
return self is not other and self.plumage() other.plumage()
def __le__(self, other):
return self is not other and self.plumage() <= other.plumage()
def __ge__(self, other):
return self is not other and self.plumage() >= other.plumage()

and George''s metaclass would have similar changes.

On the other hand, I haven''t seen this idiom in any Python code that I''ve
read, and I wonder if this was just a coding fad of the time.

Still, in cases such as Steven''s Aardark class, it might be worth bypassing
something that calls lots_of_work if you tested first to see if self is not
other.

-- Paul

这篇关于Boilerplate采用丰富的比较方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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