Overloading "if object" unary operator

Sarcastic Zombie sarcasticzombie at gmail.com
Wed Nov 29 11:17:02 EST 2006


Good morning,

If I have a class

class A:
    __init__(id)
        self.id = id

is there any way to overload the 'if' unary usage to detect if a
variable has a value?

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.

Thanks so much!

-Jason Ledbetter




More information about the Python-list mailing list