Two syntax questions (newbie)

Szabolcs szhorvat at gmail.com
Mon Apr 23 04:15:26 EDT 2007


I used Mathematica for data processing a lot and I got spoiled by its
functional programming possibilities.
I was drawn to Python because it also allows for a similar programming
style (and, more importantly, it has an interactive shell, ipython,
and a lot of libraries that are useful to me, like scipy).
I am still learning the language, so please be gentle :-)

And here comes the first question:

Mathematica allows writing
result = processData at Reverse@Sort at data
or even
result = data//Sort//Reverse//processData
instead of
result = processData[Reverse[Sort[data]]]

In Python this would be something like
result = processData(list(reversed(sorted(data))))

The first two Mma alternatives are both easier to read (especially
when part of a bigger expression) and easier to type (an re-type, if
working interactively) because I don't have to jump with the cursor to
the beginning and end of the expression to insert the brackets when
adding another function call.

Is there a way to avoid typing all the parentheses in Python? Is it
possible to define a different syntax for function calls or define a
function composition operator for functions that take a single
argument?

The second question:

Of course Python is not Mathematica and not as friendly with this
style as Mma is. Sometimes it forces me to use some temporary
variables (e.g. because lines get too long and breaking lines with a
backslash is ugly). I like to get rid of these variables as soon as
they aren't needed, e.g.:

temp = data[:]
temp.sort()
temp.reverse()
result = processData(temp)
del temp

Is it possible to avoid the explicit del temp? In C/C++ I would simply
enclose the piece of code in curly brackets. Is it possible to somehow
separate a block of code from the rest of the program and make
variables local to it?

Of course none of these two things would allow me do to something that
I can not already do, but they would make work easier. And I am
curious if they are possible.

Thanks for your replies in advance,
Szabolcs




More information about the Python-list mailing list