Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: What is the naming convention for accessor of a 'private'variable?

Reply
Thread Tools

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

 
 
Chris Rebert
Guest
Posts: n/a
 
      11-19-2009
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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-19-2009
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
 
Reply With Quote
 
 
 
 
Gregory Ewing
Guest
Posts: n/a
 
      11-21-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: What is the naming convention for accessor of a 'private'variable? Peng Yu Python 1 11-22-2009 06:53 PM
What is the naming convention for accessor of a 'private' variable? Peng Yu Python 0 11-19-2009 02:27 AM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
WS C3550 naming convention question frishack@gmail.com Cisco 2 02-23-2005 07:29 PM
Naming convention for accessor methods (get/set) Generic Usenet Account C++ 24 10-06-2003 12:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57