'if foo' calls getattr?!?

Bruce Dodson bruce_dodson at bigfoot.com
Mon Aug 30 21:59:43 EDT 1999


If what you mean is really "is something" then "if not foo is None:" is what
you should say here.  However, I think getattr is only called if the method
isn't found in the dictionaries.  If that's true, I suggest defining
__nonzero__ to avoid those expensive getattr calls, so that when someone
does say "if foo:" performance will stay reasonable.  (Anyway, sometimes "if
foo:" is the right thing to say, since there are other ways for an object to
be untrue without being None.)

Robin Becker wrote in message >It seems that it tries to call
foo.__nonzero__()  and then foo.__len__()
>to test whether foo is true in some sense.
>
>I think these are standard behaviours for class instances. I think you
>should consider using if not foo is None: pass
>--
>Robin Becker





More information about the Python-list mailing list