Re: What is the naming convention for accessor of a 'private'variable?

Discussion in 'Python' started by Chris Rebert, Nov 19, 2009.

  1. Chris Rebert

    Chris Rebert Guest

    On Wed, Nov 18, 2009 at 6:27 PM, Peng Yu <> wrote:
    > http://www.python.org/dev/peps/pep-0008/
    >
    > The above webpage states the following naming convention. Such a
    > variable can be an internal variable in a class. I'm wondering what is
    > the naming convention for the method that access such variable.
    >
    >    - _single_leading_underscore: weak "internal use" indicator.  E.g. "from M
    >      import *" does not import objects whose name starts with an underscore.


    If there's a method to access the variable, then it's not all that
    private, is it?
    Accessor methods are not Pythonic. Just make the attribute public by
    not prefixing it with an underscore.

    See also "Python is Not Java":
    http://dirtsimple.org/2004/12/python-is-not-java.html

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Nov 19, 2009
    #1
    1. Advertising

  2. On Wed, 18 Nov 2009 18:47:34 -0800, Chris Rebert wrote:

    > On Wed, Nov 18, 2009 at 6:27 PM, Peng Yu <> wrote:
    >> http://www.python.org/dev/peps/pep-0008/
    >>
    >> The above webpage states the following naming convention. Such a
    >> variable can be an internal variable in a class. I'm wondering what is
    >> the naming convention for the method that access such variable.
    >>
    >>    - _single_leading_underscore: weak "internal use" indicator.  E.g.
    >>    "from M
    >>      import *" does not import objects whose name starts with an
    >>      underscore.

    >
    > If there's a method to access the variable, then it's not all that
    > private, is it?


    True, but it might be read-only, or the accessor might do validation to
    ensure that the caller doesn't stuff a string in something expected to be
    a float, or some sort of computed attribute. That's why we have
    properties.


    > Accessor methods are not Pythonic.


    Accessor methods are *usually* not Pythonic, at least not the way they
    are commonly used in Java.

    In fact, Python has at least two built-in accessor functions:

    globals()
    locals()

    There may be others.



    --
    Steven
     
    Steven D'Aprano, Nov 19, 2009
    #2
    1. Advertising

  3. Steven D'Aprano wrote:
    > On Wed, 18 Nov 2009 18:47:34 -0800, Chris Rebert wrote:
    >
    >>Accessor methods are not Pythonic.

    >
    > Accessor methods are *usually* not Pythonic, at least not the way they
    > are commonly used in Java.


    To elaborate, accessor methods that *only* read and write
    another, ordinary attribute are completely unnecessary
    in Python. In that case there is no harm in exposing the
    underlying attribute directly, because you can always
    replace it with a property later if you change your mind,
    without requiring any change in calling code.

    Also, even when you do need accessor methods, it's more
    elegant to hide them behind a property than to expect
    callers to use them directly. It makes the calling code
    more concise, and allows for changing your mind in the
    opposite direction.

    --
    Greg
     
    Gregory Ewing, Nov 21, 2009
    #3
    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. Unpredictable

    Variable naming convention

    Unpredictable, Jan 3, 2006, in forum: Java
    Replies:
    4
    Views:
    555
    Roedy Green
    Jan 3, 2006
  2. Generic Usenet Account

    Naming convention for accessor methods (get/set)

    Generic Usenet Account, Sep 19, 2003, in forum: C++
    Replies:
    24
    Views:
    2,896
    Glen Low
    Oct 6, 2003
  3. Peng Yu
    Replies:
    3
    Views:
    1,118
    Simon Forman
    Sep 21, 2009
  4. Peng Yu
    Replies:
    0
    Views:
    311
    Peng Yu
    Nov 19, 2009
  5. Peng Yu
    Replies:
    1
    Views:
    351
    Lie Ryan
    Nov 22, 2009
Loading...

Share This Page