Does underscore has any special built-in meaningin Python ?

D

dandi kain

Hello everybody,
I have just started learning Python.I heard its simple so I pick a
presentation [1] and tried to work on it.But when it comes to
underscores leading and trailing an object I dont understand any.I
look through the python manual also but that was not helping .I
searched some forums and I still dont have a clear picture.

What is the functionality of __ or _ , leading or trailing an object ,
class ot function ? Is it just a naming convention to note special
functions and objects , or it really mean someting to Python ?

Thanks ahead ,

[1] http://www.aleax.it/goo_py4prog.pdf
 
B

Benjamin Kaplan

Hello everybody,
I have just started learning Python.I heard its simple so I pick a
presentation [1] and tried to work on it.But when it comes to
underscores leading and trailing an object I dont understand any.I
look through the python manual also but that was not helping .I
searched some forums and I still dont have a clear picture.

What is the functionality of __ or _ , leading or trailing an object ,
class ot function ? Is it just a naming convention to note special
functions and objects , or it really mean someting to Python ?

It's just a convention for the most part. A single leading underscore
is used for "private" attributes. Two leading underscores will affect
the code- it mangles the variable name so that you don't have to worry
about the value being overwritten by a subclass. For instance
"""
class Foo(object) :
def __init__(self) :
self.__bar = ''

foo = Foo()
""
will store the attribute as foo._Foo__bar.

Also, the "magic methods"- the ones that are used for operations and
built-in stuff, all have two leading and two trailing underscores.
These are things like __add__ (+), __eq__ (=), __cmp__ (old way for
comparisons), __len__ (len), __str__ (str), and so on.

 
J

Jan Kaliszewski

T

Terry Reedy

Benjamin said:
Hello everybody,
I have just started learning Python.I heard its simple so I pick a
presentation [1] and tried to work on it.But when it comes to
underscores leading and trailing an object I dont understand any.I
look through the python manual also but that was not helping .I
searched some forums and I still dont have a clear picture.

What is the functionality of __ or _ , leading or trailing an object ,
class ot function ? Is it just a naming convention to note special
functions and objects , or it really mean someting to Python ?

It's just a convention for the most part. A single leading underscore
is used for "private" attributes. Two leading underscores will affect
the code- it mangles the variable name so that you don't have to worry
about the value being overwritten by a subclass. For instance
"""
class Foo(object) :
def __init__(self) :
self.__bar = ''

foo = Foo()
""
will store the attribute as foo._Foo__bar.

Also, the "magic methods"- the ones that are used for operations and
built-in stuff, all have two leading and two trailing underscores.
These are things like __add__ (+), __eq__ (=), __cmp__ (old way for
comparisons), __len__ (len), __str__ (str), and so on.

For this last, see
http://docs.python.org/dev/py3k/reference/datamodel.html#special-method-names
 
A

alex23

What is the functionality of __ or _ , leading or trailing an object ,
class ot function ? Is it just a naming convention to note special
functions and objects , or it really mean someting to Python ?

I think everyone else has covered what you need to know, but there's
still one remaining pseudo-convention you might see in Python code,
and that's using _ as a label to indicate that you don't care about
the value it holds.

Overly simplistic example:

data_set = [('gold','junk'),('gold','junk'),...]

for keep, _ in data_set:
...

It's a convenient way of not having to come up with a label for
something you're not going to use, although arguably you get the same
effect with names like 'dummy', 'ignore' etc. Not everyone agrees with
this usage but you _will_ see it in use in other people's code, so it
helps to have a heads up.
 
B

Bruno Desthuilliers

Ben Finney a écrit :
<OP>
Please note that in Python, classes and functions are objects too. But
anyway, these underscores are part of *identifiers*, not objects
themselves !-)
foo Ordinary name, part of public interface
_foo Ordinary name, part of internal-only interface
__foo Ordinary name, but will be mangled (this style used rarely)
__foo__ Name which is used in a special way by Python

And FWIW:

foo_ When you want to use a reserved name for identifier (ie:
'class_' , 'or_', 'and_' etc)
 
A

Aahz

What is the functionality of __ or _ , leading or trailing an object ,
class ot function ? Is it just a naming convention to note special
functions and objects , or it really mean someting to Python ?

One more thing: if you have global module names with a single leading
underscore (e.g. "_foo"), they will not be loaded when you use

from <module> import *

However, "import *" is strongly discouraged for the most part.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important." --Henry Spencer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top