Does underscore has any special built-in meaningin Python ?

1 view
Skip to first unread message

dandi kain

unread,
Jul 29, 2009, 1:59:55 PM7/29/09
to
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

Benjamin Kaplan

unread,
Jul 29, 2009, 2:08:07 PM7/29/09
to pytho...@python.org

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.


>
> Thanks ahead ,
>
> [1] http://www.aleax.it/goo_py4prog.pdf

> --
> http://mail.python.org/mailman/listinfo/python-list

Jan Kaliszewski

unread,
Jul 29, 2009, 3:54:51 PM7/29/09
to Benjamin Kaplan, pytho...@python.org
29-07-2009 Benjamin Kaplan <benjami...@case.edu> wrote:

> On Wed, Jul 29, 2009 at 1:59 PM, dandi kain <dandi...@gmail.com> wrote:

[snip


>> 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-

Single leading underscore in some situations also affect the code...

See:

*
http://docs.python.org/reference/lexical_analysis.html#reserved-classes-of-identifiers

* http://docs.python.org/reference/datamodel.html#object.__del__
(in the the red "Warning" frame)

--
Jan Kaliszewski (zuo) <z...@chopin.edu.pl>

Terry Reedy

unread,
Jul 29, 2009, 3:57:25 PM7/29/09
to pytho...@python.org
Benjamin Kaplan wrote:
> On Wed, Jul 29, 2009 at 1:59 PM, dandi kain <dandi...@gmail.com> wrote:
> 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

alex23

unread,
Jul 29, 2009, 8:24:03 PM7/29/09
to
On Jul 30, 3:59 am, dandi kain <dandi.k...@gmail.com> wrote:
> 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.

Ben Finney

unread,
Jul 29, 2009, 7:42:35 PM7/29/09
to
dandi kain <dandi...@gmail.com> writes:

> What is the functionality of __ or _ , leading or trailing an object ,
> class ot function ?

There is no change in functionality. It has some slight effects on
import, but as a beginner you shouldn't need to worry about that.

> Is it just a naming convention to note special functions and objects ,
> or it really mean someting to Python ?

Both. It's a strongly-adhered-to naming convention, as an indicator to
the reader how that name should be used.

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

There's no change in the objects themselves; the underscores rather
indicate how those names are expected to be accessed.

--
\ “Pinky, are you pondering what I'm pondering?” “I think so, |
`\ Brain, but Tuesday Weld isn't a complete sentence.” —_Pinky and |
_o__) The Brain_ |
Ben Finney

Bruno Desthuilliers

unread,
Jul 30, 2009, 7:40:05 AM7/30/09
to
Ben Finney a écrit :

> dandi kain <dandi...@gmail.com> writes:
>
>> What is the functionality of __ or _ , leading or trailing an object ,
>> class ot function ?

<OP>
Please note that in Python, classes and functions are objects too. But
anyway, these underscores are part of *identifiers*, not objects
themselves !-)
</OP>

> 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)

Aahz

unread,
Jul 31, 2009, 7:09:16 PM7/31/09
to
In article <062813eb-7eec-4504...@12g2000pri.googlegroups.com>,

dandi kain <dandi...@gmail.com> wrote:
>
>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 (aa...@pythoncraft.com) <*> 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

Ben Finney

unread,
Jul 31, 2009, 11:21:05 PM7/31/09
to
aa...@pythoncraft.com (Aahz) writes:

> 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.

Dude, that's exactly the “underscore has an effect on import but as a
beginner don't worry about it” that I avoided drawing attention to. The
explanation was just fine without that. But no, you have to find a way
to *contribute*, don't you?

Things were different before all these new-fangled changes, I say. Get
off my lawn.

--
\ “I love and treasure individuals as I meet them, I loathe and |
`\ despise the groups they identify with and belong to.” —George |
_o__) Carlin, 2007 |
Ben Finney

Reply all
Reply to author
Forward
Message has been deleted
0 new messages