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

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Nov 19 22:22:15 CET 2009

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

> On Wed, Nov 18, 2009 at 6:27 PM, Peng Yu <pengyu.ut at gmail.com> 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 

> 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:


There may be others.


More information about the Python-list mailing list