[Python-ideas] proposal to add rowexpr as a keyword
jcarlson at uci.edu
Wed May 30 00:41:40 CEST 2007
Steve Howell <showell30 at yahoo.com> wrote:
> I don't mean to oversimplify this, which is why I'm
> batting it around on a forum with a lot of smart
> people. But clearly it could be done. Whether it
> *should* be done is, of course, a question for debate,
> and I accept this.
You are proposing to insert a mini-language into Python so as to make
certain relational data processing tasks easier. As you say, your
particular proposal is pretty much just an alias for lambda where you
don't specify the 'row' argument, names are acquired from the passed
dictionary, and maybe you get some free enclosing scope goodies.
Honestly, without explicit stating of where data is coming from, either
via row['column'] or row.column, and the explicit naming of the argument
to be passed (as in lambda argument: expression), it smells far too
magical for Python. Never mind the fact that you will tend to get
better performance by not using a lambda/your hacked lambda, as the
direct execution will have fewer function calls... That is...
pred = lambda row: (convert_to_euros(row['salary']) > 50000
and row['deptno'] == 10)
a = [f(i) for i in rows if pred(i)]
will run slower than
b = [f(i) for i in rows if
convert_to_euros(row['salary']) > 50000
and row['depno'] == 10]
While the former *may* be easier to read (depending on who you are), the
latter will be faster in all cases. The only way for the pred() version
to be faster is if your rowexpr: variant was actually a macro expansion,
but we don't do macro expansions in Python, so it will be slower.
So, what do we have? Possible minor clarity win (but certainly not over
lambdas), small length reduction over lambdas, slower execution compared
to inlining the code (equal to lambda), more syntax.
Put me down for -1.
More information about the Python-ideas