By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

Newb question: underscore

P: n/a
What is this doing?

print >fd, _(__doc__)
I'm guessing line-splitting __doc__ into a list, but what's that
leading underscore do?

Thanks!
Jun 27 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Skye <sp****@gmail.comwrites:
What is this doing?

print >fd, _(__doc__)
Without any context, it's impossible to know.
I'm guessing line-splitting __doc__ into a list, but what's that
leading underscore do?
Look at the rest of the module to see where that name comes from;
perhaps an assignment, or an 'import foo as _'.

My guess would be someone has used the common convention of naming the
"get the corresponding localised version of this string from the
application's gettext database" function as '_' for convenience.

That's only a guess though; you should believe the code you have in
front of you, not my guesses.

--
\ "Pinky, are you pondering what I'm pondering?" "Well, I think |
`\ so, Brain, but 'apply North Pole' to what?" -- _Pinky and The |
_o__) Brain_ |
Ben Finney
Jun 27 '08 #2

P: n/a
Skye wrote:
What is this doing?

print >fd, _(__doc__)
I'm guessing line-splitting __doc__ into a list, but what's that
leading underscore do?
It's calling a function with a single argument, like sqrt(x), except the
function is named _ and the argument is named __doc__. The underscores
have no special significance here, but they do make the code hard to read.

The first part of the statement directs the print to send the output to
a file, named fd, which was presumably opened earlier ... but I don't
think that was part of your question.

Gary Herron
Thanks!
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #3

P: n/a
Ohh, it's a function _() call. Now it makes sense.

Of course Python would be consistent... I was expecting trickery!

It's actually from the Mailman source, def _(s) is a string function
for i18n

Thanks,
Skye

Jun 27 '08 #4

P: n/a
My guess would be someone has used the common convention of naming the
"get the corresponding localised version of this string from the
application's gettext database" function as '_' for convenience.
Funny that this comes up.

I just noticed this in some code I was looking at the other day. A
number of statements in the form:

print _(something)

My question is: Why would anyone decide to obfuscate something as easy
to read as Python??? At first I thought that they were making a function
out of print (which makes some sense), but I don't think that is the
case. I tried (not very hard) to trace back the code to figure out where
_() is being assigned, but gave up. Oh, this is in the gdesklets package
if anyone is interested.
**** Listen to my CD at http://www.mellowood.ca/music/cedars ****
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: bo*@mellowood.ca
WWW: http://www.mellowood.ca

Jun 27 '08 #5

P: n/a
Skye wrote:
What is this doing?

print >fd, _(__doc__)
I'm guessing line-splitting __doc__ into a list, but what's that
leading underscore do?

Thanks!
I think it is standard practice to use the underscore for unicode converts.
Jun 27 '08 #6

P: n/a
bvdp <bo*@mellowood.cawrites:
My question is: Why would anyone decide to obfuscate something as easy
to read as Python???
They didn't decide to obfuscate; they decided to follow a
strongly-expected convention for the name of that function by existing
users of the 'gettext' functionality, in contexts that predate the
appearance of that functionality in Python.

--
\ Hercules Grytpype-Thynne: "Well, Neddie, I'm going to be |
`\ frank." Ned Seagoon: "Right, I'll be Tom." Count Moriarty: |
_o__) "I'll be Gladys." *slap* -- The Goon Show, _World War I_ |
Ben Finney
Jun 27 '08 #7

P: n/a
My question is: Why would anyone decide to obfuscate something as easy
to read as Python???

They didn't decide to obfuscate; they decided to follow a
strongly-expected convention for the name of that function by existing
users of the 'gettext' functionality, in contexts that predate the
appearance of that functionality in Python.
Well _ can also mean the previous output statement that wasn't null,
so it has OTHER uses...
Jun 27 '08 #8

P: n/a
co*********@gmail.com a écrit :
>>My question is: Why would anyone decide to obfuscate something as easy
to read as Python???
They didn't decide to obfuscate; they decided to follow a
strongly-expected convention for the name of that function by existing
users of the 'gettext' functionality, in contexts that predate the
appearance of that functionality in Python.

Well _ can also mean the previous output statement that wasn't null,
In the shell only IIRC.

Jun 27 '08 #9

P: n/a
John Fabiani a écrit :
Skye wrote:
>What is this doing?

print >fd, _(__doc__)
I'm guessing line-splitting __doc__ into a list, but what's that
leading underscore do?

Thanks!
I think it is standard practice to use the underscore for unicode converts.
Actually, it's for i18n, not for encoding.
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.