Discussion:
What is the naming convention for accessor of a 'private' variable?
(too old to reply)
Peng Yu
2009-11-19 02:27:47 UTC
Permalink
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.
Chris Rebert
2009-11-19 02:47:34 UTC
Permalink
Post by Peng Yu
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
Steven D'Aprano
2009-11-19 21:22:15 UTC
Permalink
Post by Chris Rebert
Post by Peng Yu
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.
Post by Chris Rebert
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
Gregory Ewing
2009-11-21 01:16:52 UTC
Permalink
Post by Steven D'Aprano
Post by Chris Rebert
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
Benjamin Kaplan
2009-11-19 02:50:14 UTC
Permalink
Post by Peng Yu
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.
--
You never have any convention for a method that accesses the private
variable. In a language that has scope declarations, a method that
uses private variables is still declared public or private- there's no
special keyword for it.
Post by Peng Yu
http://mail.python.org/mailman/listinfo/python-list
Ben Finney
2009-11-19 03:01:45 UTC
Permalink
Post by Peng Yu
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.
A property <URL:http://docs.python.org/library/functions.html#property>
named with a regular public attribute name::

class Foo(object):

def __init__(self, spam):
self._spam = spam

@property
def spam(self):
return self._spam
--
\ “The only tyrant I accept in this world is the still voice |
`\ within.” —Mahatma Gandhi |
_o__) |
Ben Finney
Peng Yu
2009-11-19 03:03:02 UTC
Permalink
On Wed, Nov 18, 2009 at 8:50 PM, Benjamin Kaplan
Post by Benjamin Kaplan
Post by Peng Yu
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.
--
You never have any convention for a method that accesses the private
variable. In a language that has scope declarations, a method that
uses private variables is still declared public or private- there's no
special keyword for it.
I knew this. That's why I put quote around 'private' in the email subject.
Peng Yu
2009-11-19 03:04:46 UTC
Permalink
Post by Chris Rebert
Post by Peng Yu
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.
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."
Lie Ryan
2009-11-22 18:53:44 UTC
Permalink
Post by Peng Yu
Post by Chris Rebert
Post by Peng Yu
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.
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).

Chris Rebert
2009-11-19 03:32:03 UTC
Permalink
Post by Peng Yu
Post by Chris Rebert
Post by Peng Yu
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.
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."
#untested off-the-cuff example:

from time import time

def add_timing(func): # takes a function as an argument
"""Wraps functions so they output their execution time when called"""
def wrapper(*args, **kwargs): #defines a new function
start = time()
# thanks to closures, we can access func here
result = func(*args, **kwargs) # have the new function call
the given function
end = time()
duration = end-start
print "Call to", func.__name__, "took", duration, "seconds"
return result
return wrapper # return the new function so that it can replace
the function it calls

def long_calculation(x, y, z):
# some code

long_calculation = add_timing(long_calculation)

print long_calculation(1, 7, 42)

#Example output:
#Call to long_calculation took 135.5 seconds
#99

Now imaging applying add_timing to multiple functions and note the
amount of code you'd save and how the unrelated concerns of timing and
mathematical calculation (or whatever the functions you apply
add_timing() to do) would then be cleanly separated.
The same technique can be applied to logging and other repetitive code
to factor it out.
It's sort of like aspect-oriented programming, but not as kludgey.

Cheers,
Chris
--
http://blog.rebertia.com
Continue reading on narkive:
Loading...