Isn't bool __invert__ behaviour "strange"?
Paul McGuire
ptmcg at austin.rr._bogus_.com
Fri Sep 22 12:38:35 EDT 2006
"Saizan" <sanzhiyan at gmail.com> wrote in message
news:1158942134.691147.308530 at i42g2000cwa.googlegroups.com...
>
> John Roth wrote:
>
>> The not operator and the bool() builtin produce
>> boolean results. Since bool is a subclass of int,
>> all the integer operations will remain integer
>> operations. This was done for backwards
>> compatability, and is unlikely to change in the 2.x
>> series.
>
> Ok, shame on me, I completely overlooked "not" and it surprises myself
> because it's not like I haven't used it, I just didn't see "not" as an
> operator, maybe because i can't find a __not__ method in bool class.
> (Is it hidden somewhere or is computed in some other way?)
>
> (However (not x) whould be as annoying as 1-x even if a little more
> readable (if you consider lispish parentheses readable):
> Input expression: (not (not x)&(not y)!(not (z|v)))
> Maybe direct eval is just the wrong way of doing this, I should look
> for or make muParser bindings for Python instead..)
>
What about __nonzero__?
class IsOdd(object):
def __init__(self,n):
self.val = n
def __nonzero__(self):
return self.val % 2
for i in range(4):
if IsOdd(i):
print i,"is odd"
else:
print i,"is even"
Prints:
0 is even
1 is odd
2 is even
3 is odd
-- Paul
More information about the Python-list
mailing list