473,372 Members | 840 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,372 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 953
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 ?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...

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.