[Python-Dev] the not operator (and the __not__ special method)

Brian Quinlan brian@sweetapp.com
2002年10月03日 14:50:54 -0700


> shows that python doesn't call the __not__ special method
> in a 'not' operator statement.

Python calls the special __nonzero__ method so check the truth value of
an object.
> Another question, I notice than "a or b" and "a and b" are
> not equivalent to "a | b" and "a & b", since the last ones call
> the methods __or__ and __and__ if they are defined, but
> the "literal forms" never call them. Is this intentional?, if
> that is the case, I guess a big and clear note is needed
> in the documentation.

"and" and "or" are logical operators, while "|" and "&" are bitwise
operators. 
>>> 13 & 14
12
>>> 13 and 14
14
> And just for symmetry considerations, given that python has the
> pairs (and, &) and (or, |), does anybody considering to introduce
> the ! operator, so you can have the equivalent (not, !) too?

There is a bitwise not operator: ~
>>> ~2
-3
Cheers,
Brian

AltStyle によって変換されたページ (->オリジナル) /