Connecting Tech Pros Worldwide Help | Site Map

Newb question: underscore

Skye
Guest
 
Posts: n/a
#1: Jun 27 '08
What is this doing?

print >fd, _(__doc__)


I'm guessing line-splitting __doc__ into a list, but what's that
leading underscore do?

Thanks!
Ben Finney
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Newb question: underscore


Skye <spoier@gmail.comwrites:
Quote:
What is this doing?
>
print >fd, _(__doc__)
Without any context, it's impossible to know.
Quote:
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
Gary Herron
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Newb question: underscore


Skye wrote:
Quote:
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
Skye
Guest
 
Posts: n/a
#4: Jun 27 '08

re: Newb question: underscore


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

bvdp
Guest
 
Posts: n/a
#5: Jun 27 '08

re: Newb question: underscore


Quote:
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: bob@mellowood.ca
WWW: http://www.mellowood.ca

John Fabiani
Guest
 
Posts: n/a
#6: Jun 27 '08

re: Newb question: underscore


Skye wrote:
Quote:
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.
Ben Finney
Guest
 
Posts: n/a
#7: Jun 27 '08

re: Newb question: underscore


bvdp <bob@mellowood.cawrites:
Quote:
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
cokofreedom@gmail.com
Guest
 
Posts: n/a
#8: Jun 27 '08

re: Newb question: underscore


My question is: Why would anyone decide to obfuscate something as easy
Quote:
Quote:
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...
Bruno Desthuilliers
Guest
 
Posts: n/a
#9: Jun 27 '08

re: Newb question: underscore


cokofreedom@gmail.com a écrit :
Quote:
Quote:
Quote:
>>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.

Bruno Desthuilliers
Guest
 
Posts: n/a
#10: Jun 27 '08

re: Newb question: underscore


John Fabiani a écrit :
Quote:
Skye wrote:
>
Quote:
>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.
Closed Thread