[FRPythoneers] Python documentation and default expression

Jeffery D. Collins jcollins at boulder.net
Fri Mar 15 15:14:50 MST 2002


Check out the following:

http://www.python.org/doc/current/ref/lambda.html

The key paragraph of that link appears to be:

"In the context of Boolean operations, and also when expressions are used by
control flow statements, the following values are interpreted as false:
None, numeric zero of all types, empty sequences (strings, tuples and
lists), and empty mappings (dictionaries). All other values are interpreted
as true."

So, when you added the __len__ method, your instance became a sequence.  In
your example below, if foo is not None and the __len__ returns 0, then the
test will fail.

I've been burned by unexpected behavior like this before, and so I try to
keep the expression explicit, such as:

if foo is not None:
    blah..


--
Jeffery Collins (http://www.boulder.net/~jcollins)

----- Original Message -----
From: "Rob Riggs" <rob at pangalactic.org>
To: "Front Range Pythoneers" <frpythoneers at lists.community.tummy.com>
Sent: Friday, March 15, 2002 2:39 PM
Subject: [FRPythoneers] Python documentation and default expression


> Where do I find Pythons rules for the default tests on a variable? I.e.,
> when I have an expressions like:
>
>     if foo: ...
>
> what are the tests on "foo" that are performed? Is this documented in
> the language reference anywhere?
>
> Along the same lines, what are the thoughts on using this type of test
> in code? The reason I ask is that I was just burned on a test like this
> when I extended a class to include a __len__() member function. What I
> really wanted to test was that the class wasn't "None", and using the
> above idiom worked well for that. At least it did until I added __len__().
>
> -Rob
>
>
> _______________________________________________
> This message sent by the FRPythoneers mailing list.
> Unsubscribe: echo unsubscribe |
FRPythoneers-request at lists.community.tummy.com
> URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers
>




More information about the FRPythoneers mailing list