ACCEPTED: PEP 285

Hernan M. Foffani hfoffani at yahoo.com
Thu Apr 4 08:00:49 EST 2002


"Ralf Juengling"
> this might have been brought up already (I didn't follow the whole
> discussion): Currently, when an object x is tested as in
> 'if x:' or 'if not x:'
>
> the interpretation of x as a boolean is done by PyObject_IsTrue()
> at the very end. PyObject_IsTrue() itself tries to interpret the
> given object as a number, then (if this fails) as a mapping,
> finally as a sequence. Here is where the something/nothing semantic
> roots in.
>
> But what about user-defined types?
>

__nonzero__() ?


class YepNop:
    def __init__(self, arg):
        self.val = arg

    def __nonzero__(self):
        if self.val == "yep":
            return 1
        else:
            return 0

yes = YepNop("yep")
no  = YepNop("nop")
if yes:
    print "yes"
if not no:
    print "no"


Regards,
-Hernan






More information about the Python-list mailing list