[FRPythoneers] Python documentation and default expression

Evelyn Mitchell efm at tummy.com
Fri Mar 15 14:54:34 MST 2002

* On 2002-03-15 21:41 Rob Riggs <rob at pangalactic.org> wrote:
> 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?

I took a quick look at http://www.python.org/doc/current/ref/grammar.txt
It looks like foo is an expression (which caches out to an or_test or a 
lambda_form and so on). If you trace back through the grammar, it looks
to depend entirely on what sort of thing foo is.

If I was look at this, and foo was a number, then if it isn't 0, I'd
know it was true. If foo was a string, I'd know what it meant. 

If foo is a class or foo is a method I don't know what it would mean for
it to be false. If it isn't a simple object, then what are you intending
to check?  Does it mean that the class doesn't have any instantiated

Does it mean that the method isn't defined?

> 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__().

I'm hoping someone else will give a better answer than this.

Regards,                    tummy.com, ltd 
Evelyn Mitchell             Linux Consulting since 1995
efm at tummy.com               Senior System and Network Administrators

More information about the FRPythoneers mailing list