473,489 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Solved: Question about idiomatic use of _ and private stuff.

On Friday, Feb 23rd 2007 at 11:12 -0500, quoth Steven W. Orr:

=>I understand that two leading underscores in a class attribute make the
=>attribute private. But I often see things that are coded up with one
=>underscore. Unless I'm missing something, there's a idiom going on here.
=>
=>Why do people sometimes use one leading underscore?

I found the answer. It turns out that if you say:

import foo

then you get access to all of the attributes that are not mangled. A
single leading underscore does not cause mangling.

If you say

from foo import _fooa, _foob,

then the import will fail because the _ is used only by the import to
decide that you shouldn't see _fooa or _foob.

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Feb 24 '07 #1
2 1164
On Fri, 23 Feb 2007 23:17:32 -0500, Steven W. Orr wrote:
On Friday, Feb 23rd 2007 at 11:12 -0500, quoth Steven W. Orr:

=>I understand that two leading underscores in a class attribute make the
=>attribute private. But I often see things that are coded up with one
=>underscore. Unless I'm missing something, there's a idiom going on here.
=>
=>Why do people sometimes use one leading underscore?

I found the answer.
Not quite.

It turns out that if you say:

import foo

then you get access to all of the attributes that are not mangled. A
single leading underscore does not cause mangling.

If you say

from foo import _fooa, _foob,

then the import will fail because the _ is used only by the import to
decide that you shouldn't see _fooa or _foob.
Incorrect.

Let's try it. From the shell:

$ cat data.py
fear = "The chief weapon of the Spanish Inquisition"
_parrot = "A green bird"
__spam = "A nasty treat"
And then from Python:
>>from data import fear, _parrot, __spam
fear
'The chief weapon of the Spanish Inquisition'
>>_parrot
'A green bird'
>>__spam
'A nasty treat'

Python only ignores _names when you call "from module import *".

Here are the underscore rules:

(1) In the interactive interpreter, the name "_" is automatically set to
the result of the last command.

(2) Names with a SINGLE lead underscore are ignored when you say "from
module import *". They are imported if you ask for them directly, and in
the normal "import module" form.

(3) Class attributes, but not other objects, with TWO leading underscores
have their names mangled by Python. E.g. Parrot.__method is mangled to
Parrot._Parrot__method.

(4) Names with two leading underscores and two trailing underscores may be
reserved for special methods (e.g. __init__, __str__, etc.) and objects
(e.g. __all__ in packages, __name__, etc.). While Python doesn't prohibit
you from creating your own methods with leading and trailing underscores,
it is discouraged.

(5) If you want to give an object a name which clashes with a built-in,
the convention is to append a single TRAILING underscore to the name (e.g.
print_).
Hope that's clear now.

--
Steven.

Feb 24 '07 #2
If you say
>
from foo import _fooa, _foob,

then the import will fail because the _ is used only by the import to
decide that you shouldn't see _fooa or _foob.
???

Read Python manuals, please.

Feb 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
18799
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
3
2074
by: T.T.H. | last post by:
Hi I am coding a C++ COM object which I want to access in Python. For that I do have some detail questions and need help since I am not that familiar with the documentation itself of Python and...
125
14532
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
8
1553
by: Al Reynolds | last post by:
Afternoon, In an earlier thread (http://tinyurl.com/5v4aa), I described a problem I was having which was rather bizarrely solved by changing the line: "inputbox.value = numq+ag-cw-cc;" to:...
20
18305
by: Mark Harrison | last post by:
So I have some data that I want to put into a table. If the row already exists (as defined by the primary key), I would like to update the row. Otherwise, I would like to insert the row. I've...
10
1549
by: Miro | last post by:
I wanted certain text boxes ( only certain ones ) to always be Trim'd so that spaces are not in the begining, nor the end of the text entered. I created my own "Handle ?" - i hope thats the...
90
3360
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
4
1153
by: Steven W. Orr | last post by:
I understand that two leading underscores in a class attribute make the attribute private. But I often see things that are coded up with one underscore. Unless I'm missing something, there's a...
12
1461
by: Matt B | last post by:
I was just wondering if there is a "best" choice from the following couple of ways of returning a value from a method: 1) private HashAlgorithm GetSpecificHashAlgorithm(string hashString){ if...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4875
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.