Proto-PEP: Overloadable Boolean Operators

Alex Martelli aleaxit at yahoo.com
Mon Sep 6 09:21:12 EDT 2004


Michael Hudson <mwh at python.net> wrote:

> aleaxit at yahoo.com (Alex Martelli) writes:
> 
> > greg <greg at cosc.canterbury.ac.nz> wrote:
> >    ...
> > > Python does not currently provide any '__xxx__' special methods
> > > corresponding to the 'and', 'or' and 'not' boolean operators. In the
> >    ...
> > > There is no such difficulty in the case of 'not', however, and it
> > 
> > Indeed, that's what the strangely-named __nonzero__ special method does:
> > it's invoked upon the 'not' operator and in no other case, 
> 
> Erm.  No.
> 
> >>> class C(object):
> ...  def __nonzero__(self):
> ...   print 'hi!'
> ...   return 1
> ... 
> >>> if C(): pass
> ... 
> hi!
> 
> Or do I misunderstand what you're saying?

Oops, no, you're right, I was wrong -- not sure why I had that
misconception in my mind right now...


Alex



More information about the Python-list mailing list