Python style of accessing bools?

Discussion in 'Python' started by Jonathon McKitrick, Apr 15, 2004.

  1. 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.
    Jonathon McKitrick, Apr 15, 2004
    #1
    1. Advertising

  2. > 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
    Diez B. Roggisch, Apr 15, 2004
    #2
    1. Advertising

  3. Jonathon McKitrick

    Peter Hansen Guest

    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
    Peter Hansen, Apr 15, 2004
    #3
  4. Jonathon McKitrick

    Roy Smith Guest

    In article <>,
    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>
    >
    > 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.
    Roy Smith, Apr 15, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ccs

    An array of bools

    ccs, Jun 13, 2004, in forum: C++
    Replies:
    5
    Views:
    562
    AngleWyrm
    Jun 13, 2004
  2. Mike
    Replies:
    3
    Views:
    633
  3. Replies:
    15
    Views:
    859
    James Kanze
    Aug 3, 2008
  4. Angus
    Replies:
    20
    Views:
    713
    Krice
    Jun 26, 2009
  5. KONTRA Gergely

    types? casting and bools

    KONTRA Gergely, Dec 12, 2003, in forum: Ruby
    Replies:
    4
    Views:
    157
    KONTRA Gergely
    Dec 13, 2003
Loading...

Share This Page