Overloading "if object" unary operator

Roberto Bonvallet Roberto.Bonvallet at cern.ch
Wed Nov 29 11:24:03 EST 2006


Sarcastic Zombie wrote:
> For example, in the code:
> 
> a = A(56)
> if a:
>    print "Hoo hah!"
> 
> how can I insure that the if will come back true and fire off the print
> if and only if self.id is defined? I want to do this in an overloaded,
> generic way, if possible; I know that I could test for a.id.

Define a method called __nonzero__ that returns True or False.

-- 
Roberto Bonvallet



More information about the Python-list mailing list