Python style of accessing bools?

J

Jonathon McKitrick

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>

OR

if MyClass.IsTrueProp():
<continue>

In other words, do you use accessors or just access the variable directly?

jm
 
D

Diez B. Roggisch

In other words, do you use accessors or just access the variable directly?

As a lot of things in programming, its a matter of taste. Now I personally
think that using accessors that simply return the value without any
computation are useless, as python has no real concept of private/protected
members.

Now if someone needs to protect her members from unwanted access, IMHO
properties are the way to go.

And I don't like the overhead of defining additional accessors - they're
sort of clumsy and they don't allow for nice assignments like this:

foo.bar = <whatever>
 
P

Peter Hansen

Jonathon said:
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
 
R

Roy Smith

Jonathon McKitrick said:
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>

OR

if MyClass.IsTrueProp():
<continue>

In other words, do you use accessors or just access the variable directly?

jm

My general habit is to avoid accessor functions and grab the value
directly. It's just simplier. Writing accessor functions is (IMHO)
just mindless busywork, and more code that can break (Yes, I've written
accessor functions which had bugs!)

If you read up on the __getattr__() stuff, you'll discover that
accessing the attribute may actually end up calling a user-defined
function anyway, which may set your thoughts about private data on its
ear.

The classic argument for private data and public accessor functions is
that you can change the underlying data store and keep the interface the
same. It turns out you can do the same thing in Python with
__getattr__(). If you need to change the way the data is stored, you
can delete the attribute from your object, catch references to the (now,
non-existant) attribute with __getattr__(), and continue to export the
same interface you did before.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,578
Latest member
LC_06

Latest Threads

Top