Python style of accessing bools?

Peter Hansen peter at engcorp.com
Thu Apr 15 10:11:12 EDT 2004


Jonathon McKitrick wrote:

> I'm trying to avoid leftover C-style in my new Python programs.
> 
> If a class has a boolean property, and I am branching based on that
> property, which of these is preferred?
> 
> if MyClass.boolProp:
> 	<continue>

This is preferred.

> OR
> 
> if MyClass.IsTrueProp():
> 	<continue>
> 
> In other words, do you use accessors or just access the variable directly?

Go with direct access.  If you need to change that in the future
you can switch the attribute to a property and have the equivalent
of isTrueProp() called automatically without having to change any
calling code.

-Peter



More information about the Python-list mailing list