471,356 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,356 software developers and data experts.

variable naming query

I'ma a newbie python user and would like clarification on variable
naming conventions.

What is the difference between

self.myvariable
self._myvariable
self.__myvariable

and when should I use each of them?

Jul 12 '07 #1
2 905
loial <jl********@googlemail.comwrites:
What is the difference between

self.myvariable
Indicates to the reader that the attribute 'myvariable' is available
for use as part of the interface of the object.

Prefer this style unless you have good reason in a particular case to
do otherwise.
self._myvariable
Indicates to the reader that the attribute '_myvariable' should not be
used as part of the interface to the object.
self.__myvariable
Indicates to the reader that the attribute '__myvariable' is not
available by that name outside the object, and name mangling is
automatically done to discourage its use from outside the object.
and when should I use each of them?
Use each of them to indicate the above conditions where appropriate.

Note that none of them will change the nature of the attribute, and
Python will allow use of any of them by the correct name. There is no
such thing as "limited-access" attributes in Python; we rely on the
maxim that "we're all consenting adults here". If an attribute exists
in the current scope, it is available for any use regardless of what
name you give it.

--
\ "I wish there was a knob on the TV to turn up the intelligence. |
`\ There's a knob called 'brightness' but it doesn't work." -- |
_o__) Eugene P. Gallagher |
Ben Finney
Jul 12 '07 #2
On 2007-07-12, Ben Finney <bi****************@benfinney.id.auwrote:
>self.__myvariable

Indicates to the reader that the attribute '__myvariable' is
not available by that name outside the object, and name
mangling is automatically done to discourage its use from
outside the object.
From _Python Reference Manual (2.3.2) Reserved Classes of
Identifiers:

__*

Class-private names. Names in this category, when used
within the context of a class definition, are re-written to
use a mangled form to help avoid name clashes between
``private'' attributes of base and derived classes.

Further, from the _Python Tutorial (9.6) Private Variables_:

(Buglet: derivation of a class with the same name as the base
class makes use of private variables of the base class
possible.)

In other words, it's a misfeature that's best avoided.

--
Neil Cerutti
Jul 13 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by BillyTheKid | last post: by
6 posts views Thread by Michael | last post: by
1 post views Thread by mmiikkee | last post: by
17 posts views Thread by Control Freq | last post: by
6 posts views Thread by teddysnips | last post: by
36 posts views Thread by pereges | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.