[Python-Dev] the not operator (and the __not__ special method)
Marcelo Matus
mmatus@dinha.acms.arizona.edu
Thu, 03 Oct 2002 14:27:48 -0700
Hello:
The following test code:
class Hello:
def __not__(self):
return "calling Hello.__not__"
def __neg__(self):
return "calling Hello.__neg__"
a = Hello()
print -a
print not a
shows that python doesn't call the __not__ special method
in a 'not' operator statement.
The Modules/operator.c file, line 234, says:
spam2(neg,__neg__, "neg(a) -- Same as -a.")
spam2(pos,__pos__, "pos(a) -- Same as +a.")
spam2(abs,__abs__, "abs(a) -- Same as abs(a).")
spam2(inv,__inv__, "inv(a) -- Same as ~a.")
spam2(invert,__invert__, "invert(a) -- Same as ~a.")
spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
spam2(not_,__not__, "not_(a) -- Same as not a.")
so, one can expect that like "-a" is equivalent ot "a.__neg__()",
then "not a" is equivalent to "a.__not__()", right?
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.
Maybe the reason that the literal forms (and,or) are not equivalent to
the operator forms(&,|) is preventing python to porperly
resolve the "not" operation.
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?
Marcelo