[Python-ideas] proposal to add rowexpr as a keyword
showell30 at yahoo.com
Tue May 29 23:55:07 CEST 2007
--- Blake Winton <bwinton at latte.ca> wrote:
> Steve Howell wrote:
> > Let the Python interpreter build expression
> > for you. You write:
> > rowexpr: convert_to_euros(salary) > 50000 and
> > deptno = 10
> > Python translates that into the same bytecode as:
> > lambda row: convert_to_euros(row['salary'] > 50000
> > row['deptno'] == 10
My language here was imprecise. I should have said
something to the effect of this:
The interpreter would translate the "rowexpr" into
bytecode that would execute the same way as the
"lambda" example given the scope of the method
convert_to_euros in the example above.
Also, the bytecode would not be the "same" as the
lambda; it would just have similar complexity.
> I'm sorry, why would that not be translated into:
> lambda row: row['convert_to_euros'](row['salary'] >
> row['and'] row['deptno'] == row['10']
I am suggesting that in a rowexpr expression, instead
of the typical local scope being the default scope,
"row" would be the local scope for dereferencing.
Since I am proposing an expression syntax, does not
the local scope go away?
If you look at where salary, row, and deptno sit in
the expression, it seems pretty clear to me that a
well-defined scoping scheme would try to dereference
row by default for all those tokens, and probably give
up there, throwing an exception.
The convert_to_euros token in the expression obviously
poses a thornier problem. Do you prefer
row.convert_to_euros, if such a think exists, or do
you go right to what would be the typical scope of a
similar lambda expression, and prefer, say, the
definition of "def convert_to_euros" that sits at the
module level? I would think the latter.
The interpreter surely can evaluate such an expression
and determine that convert_to_euros is going to be
used a callable. Now, whether convert_to_euros is
actually callable at run time is another story, but if
it isn't, an exception is thrown, and at least you're
not debugging somebody else's library to figure out
why. Python would just tell you.
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.
____________________________________________________________________________________Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
More information about the Python-ideas