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

Discussion in 'Python' started by Peng Yu, Nov 19, 2009.

  1. Peng Yu

    Peng Yu Guest

    On Wed, Nov 18, 2009 at 8:47 PM, 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?
    > 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


    I don't quite understand the following paragraph from the above
    webpage. Would you please give me an example to help me understand it?

    "Here's what you do. You write a function that contains a function.
    The inner function is a template for the functions that you're writing
    over and over again, but with variables in it for all the things that
    vary from one case of the function to the next. The outer function
    takes parameters that have the same names as those variables, and
    returns the inner function. Then, every place where you'd otherwise be
    writing yet another function, simply call the outer function, and
    assign the return value to the name you want the "duplicated" function
    to appear. Now, if you need to change how the pattern works, you only
    have to change it in one place: the template."
    Peng Yu, Nov 19, 2009
    #1
    1. Advertising

  2. Peng Yu

    Lie Ryan Guest

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

    Peng Yu wrote:
    > On Wed, Nov 18, 2009 at 8:47 PM, 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?
    >> 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

    >
    > I don't quite understand the following paragraph from the above
    > webpage. Would you please give me an example to help me understand it?
    >
    > "Here's what you do. You write a function that contains a function.
    > The inner function is a template for the functions that you're writing
    > over and over again, but with variables in it for all the things that
    > vary from one case of the function to the next. The outer function
    > takes parameters that have the same names as those variables, and
    > returns the inner function. Then, every place where you'd otherwise be
    > writing yet another function, simply call the outer function, and
    > assign the return value to the name you want the "duplicated" function
    > to appear. Now, if you need to change how the pattern works, you only
    > have to change it in one place: the template."


    Basically it sums to (pun not intended):

    def adder(by):
    def _adder(num):
    return num + by
    return _adder

    add4 = adder(4)
    add10 = adder(10)
    add35 = adder(35)
    add1 = adder(1)

    in java, you'll need to manually duplicate the code, equivalent to doing:

    def add4(num):
    return num + 4
    def add10(num):
    return num + 10
    def add35(num):
    return num + 35
    def add1(num):
    return num + 1

    or passes two arguments (which completely misses the point).
    Lie Ryan, Nov 22, 2009
    #2
    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:
    539
    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,805
    Glen Low
    Oct 6, 2003
  3. Peng Yu
    Replies:
    3
    Views:
    1,075
    Simon Forman
    Sep 21, 2009
  4. Peng Yu
    Replies:
    0
    Views:
    290
    Peng Yu
    Nov 19, 2009
  5. Chris Rebert
    Replies:
    2
    Views:
    357
    Gregory Ewing
    Nov 21, 2009
Loading...

Share This Page