473,395 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 955
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: VM | last post by:
What's the correct variable naming in C#? How should a form be called (eg. Frm_myForm)? or an int variable (eg. Int_Var) or a DataGrig (eg. DG_myGrid)? I'm writing an application but I would like...
1
by: BillyTheKid | last post by:
I've noticed that there are many who like to prefix variable names with m (module/member), n (number), s (string), g (global), etc., however this seems to be unnecessary with C# IMO. Does anyone...
6
by: Michael | last post by:
Hi, What is the naming convention in .NET when it comes to naming a variable like "StudentID"? Is it "StudentID" or "StudentId"? For variable like "myHtml", I know the "HTML" part must be...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
1
by: mmiikkee | last post by:
Can someone direct me to a definative source to explain or suggest naming datamart or summary tables? Given a column in a detail table like revenue, customer no, date (e.g., revnu,cust_no, and...
17
by: Control Freq | last post by:
Hi, Not sure if this is the right NG for this, but, is there a convention for the variable names of a Session variable? I am using .NET 2.0 in C#. I am new to all this .NET stuff, So, any...
3
Minion
by: Minion | last post by:
I hate to even post this message as I probably know the answer before I begin. Still there are several problems facing me with this particular challenge. First though I'll goto into the overview...
6
by: teddysnips | last post by:
I've been asked to take over maintenance of a database. The client has also requested that I split the database into Front End/Back End. I've just taken delivery of the code and I've run it and it...
36
by: pereges | last post by:
Apart from the some rules set by the C standard what , in your opinion, are good naming convetions for variables ? Can you please give an example or two ?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.