syntax questions

Chris Liechti cliechti at gmx.net
Wed Jul 24 12:50:07 EDT 2002


Philippe Gendreau <philippe.gendreau at savoirfairelinux.com> wrote in 
news:mailman.1027526320.10233.python-list at python.org:

> Hi!
> I'm fairly new to python and there a thing I don't understand in the
> syntax. What is it with the "_". I have seen it used in many places for
> what seems to be different puposes like:
> 
> from x import _
> 
> Also, I'd like to understand this syntax: "((_(name),))"
> here's an example where it's used:
> 
>         # stuff a list
>         for dir in os.listdir('/usr/share/themes'):
>             row = self.theme.append ((_(dir),))
>             self.theme.set_row_data (row, dir)
> 
> More precisely, what are the comma and underscore for?

you want to call "append" on the variable "self.theme" which seems to be a 
list, therefore the outher parantheses:
self.theme.append(...)

you want to add a tuple to the list:
(...,)

a tuple with just one argument needs a coma at the end, otherwise it would 
be an arithmetic expression ans no tupple.

and finaly you call a function with the name "_":
_(dir)

that is sometimes used to localize names i.e. the "_" function translates 
the given string to the local language. to be sure what it does you have to 
search for the implementation in you current sourcecode.

note that "dir" is somewhat a bad name for that variable because
a) there is a builtin function that name that you hide
b) its not a directory name but a filename without path in the above case

in the intercative prompt "_" is bound to the last result so that you can 
do further calculations:
>>> 1+1
2
>>> _*3
6

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list