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 ,
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
> 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>
For this last, see
http://docs.python.org/dev/py3k/reference/datamodel.html#special-method-names
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.
> 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
<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)
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
> 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