覆盖'和'和'或' [英] Override 'and' and 'or'

查看:107
本文介绍了覆盖'和'和'或'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以覆盖''和''和/或''或''?我找不到一个特殊的

方法... __and__和__rand__和__or__和__ror__用于

二元操作...任何提案?


有美妙的星期天,

Marco

解决方案

Dekker< m。* *********@gmail.com写道:


是否可以覆盖''和''和/或''或''?我找不到一个特殊的

方法... __and__和__rand__和__or__和__ror__用于

二元操作...任何提案?



如果你想自定义真值测试你必须实现

__nonzero__




__nonzero __(self)

调用实现真值测试,内置操作

bool();应该返回False或True,或它们的等价整数0或

1.如果没有定义此方法,则调用__len __(),如果它是定义

(见下文) )。如果一个类既没有定义__len __()也没有定义
__nonzero __(),那么它的所有实例都被认为是真的。

"


请记住__len__和__nonzero__

ps之间的关系。为什么你需要定制这样的东西?


-

劳伦斯,oluyede.org - neropercaso.it

"很难让一个男人理解

当他的工资取决于不是

理解它时 - Upton Sinclair


Dekkeraécrit:


是否可以覆盖''和''和/还是''或''?我找不到一个特殊的

方法... __and__和__rand__和__or__和__ror__用于

二元操作...任何提案?

http://docs.python.org/ ref / customization.html

""

__nonzero __(self)

被调用以实施真值测试,和内置操作

bool();应该返回False或True,或它们的等价整数0或

1.如果没有定义此方法,则调用__len __(),如果它是定义

(见下文) )。如果一个类既没有定义__len __()也没有定义
__nonzero __(),那么它的所有实例都被认为是真的。

"""


不是在Python中,''和''不会产生bool:


>>" aa"和bb



''bb''

< blockquote class =post_quotes>
>>" aa"和无#产生无
aa和0



0


>>" aa"或bb或bb。



''a'''

< blockquote class =post_quotes>
>> 0或bb



''bb''

< blockquote class =post_quotes>
>>无或bb



''bb''

< blockquote class =post_quotes>
>>" aa"或0



''a''


>>" aa"或无



''a'''


>>>



HTH


On Sun,2007年10月7日13:52: 15 +0000,Dekker写道:


是否可以覆盖''和''和/或''或''?



不是没有破解Python源代码,在这种情况下你得到的东西是
不再是Python。


你为什么要这样做?

-

史蒂文。


Is it possible to override ''and'' and/or ''or''? I cannot find a special
method for it... __and__ and __rand__ and __or__ and __ror__ are for
binary manipulation... any proposals?

Have marvelous sunday,
Marco

解决方案

Dekker <m.**********@gmail.comwrote:

Is it possible to override ''and'' and/or ''or''? I cannot find a special
method for it... __and__ and __rand__ and __or__ and __ror__ are for
binary manipulation... any proposals?

If you want to customize the truth value testing you have to implement
__nonzero__

"
__nonzero__( self)
Called to implement truth value testing, and the built-in operation
bool(); should return False or True, or their integer equivalents 0 or
1. When this method is not defined, __len__() is called, if it is
defined (see below). If a class defines neither __len__() nor
__nonzero__(), all its instances are considered true.
"

Keep in mind the relation between __len__ and __nonzero__

ps. why you need to customize such a thing?

--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair


Dekker a écrit :

Is it possible to override ''and'' and/or ''or''? I cannot find a special
method for it... __and__ and __rand__ and __or__ and __ror__ are for
binary manipulation... any proposals?

http://docs.python.org/ref/customization.html
"""
__nonzero__( self)
Called to implement truth value testing, and the built-in operation
bool(); should return False or True, or their integer equivalents 0 or
1. When this method is not defined, __len__() is called, if it is
defined (see below). If a class defines neither __len__() nor
__nonzero__(), all its instances are considered true.
"""

Not that in Python, ''and'' don''t yield bools:

>>"aa" and "bb"

''bb''

>>"aa" and None # yields None
"aa" and 0

0

>>"aa" or "bb"

''aa''

>>0 or "bb"

''bb''

>>None or "bb"

''bb''

>>"aa" or 0

''aa''

>>"aa" or None

''aa''

>>>

HTH


On Sun, 07 Oct 2007 13:52:15 +0000, Dekker wrote:

Is it possible to override ''and'' and/or ''or''?

Not without hacking the Python source code, in which case what you''ve got
is no longer Python.

Why do you want to do so?
--
Steven.


这篇关于覆盖'和'和'或'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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