variable naming query

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Jul 12 14:38:24 CEST 2007


loial <jldunn2000 at googlemail.com> writes:

> 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



More information about the Python-list mailing list