Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python style of accessing bools?

Reply
Thread Tools

Python style of accessing bools?

 
 
Jonathon McKitrick
Guest
Posts: n/a
 
      04-15-2004

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 other computer is your Windows box.

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      04-15-2004
> 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>



--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      04-15-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
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      04-15-2004
In article <(E-Mail Removed)>,
Jonathon McKitrick <(E-Mail Removed)> 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>
>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
good style guides for python-style documentation ? Fredrik Lundh Python 4 04-07-2006 06:19 AM
DataGrid header style inconsistent with sortable column style cedoucette@alum.rpi.edu ASP .Net 0 10-14-2005 12:13 AM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
New style class & Old style class in Python dlo_olb Python 2 06-25-2003 01:46 PM



Advertisments