Re: [Python-ideas] 'where' statement in Python?

MRAB wrote:
Why use 'as'? Why not:
i would use as because this whole where clause acts very similarly to a context manager in that it sets a variable to a value for a small block
c = a + b with:
get_a(), get_b() as a, b
is equivilent to
with get_a(), get_b() as a, b:
c = a + b
assuming get_a() and get_b() are context manager that return the values of a and b and then delete them at the end
the thing with this though it that the get_a() and get_b() do not need to be context managers the interpreter will create one so this will be valid:
c = a**2 with:
4 as a
i.e. it creates a context manager whose __enter__ method returns 4 and whose exit method deletes a from the current namespace
strula moldan wrote:
sorry haven't used 'as' in a while and i forgot that you could not use it like you can everything else in compound assignments (BTW why isn't a, b as c, d allowed it makes sense especially since a, b = c, d is allowed it should be changed so it does)
c = sqrt(a*a + b*b) with:
get_a() as a
i think you miss the point which is understandable considering such a short example but consider if there are many many steps used to arrive at the variable. you can give it a simple, descriptive name and nobody needs to see how you got it unless they want to. or a situation where you have many many variables in the with statement and it is possible to understand from their names what they represent. i.e. sea = water() with (get_temperature(sea) as temp, get_depth(sea) as depth, get_purity(sea) as purity, get_salinity(sea) as saltiness, get_size(sea) as size #etc for a few more lines get_density(sea) as density): sea_num = temp**depth + purity - salinity * size - density # one line only is much harder to understand than simply sea_num = temp**depth + purity - salinity * size - density with: # one line only get_temperature(sea) as temp, get_depth(sea) as depth, get_purity(sea) as purity, get_salinity(sea) as saltiness, get_size(sea) as size #etc for a few more lines get_density(sea) as density the meaning of all the words is obvious (assuming you know that sea_num has to do with the sea and water) and since it is you do not really need to have all that clutter up above and can just put it below
On Tue, Jul 20, 2010 at 3:15 PM, MRAB <python@mrabarnett.plus.com> wrote:
Alex Light wrote:

On Wed, Jul 21, 2010 at 6:13 AM, Alex Light <scialexlight@gmail.com> wrote:
No, the idea is for the indented suite to be a perfectly normal suite of Python code. We want to be able to define functions, classes, etc in there. Inventing a new mini-language specifically for these clauses would be a bad idea (and make them unnecessarily hard to understand) For the record, I've updated the PEP* based on the discussion in this thread (I switched to "given" as the draft keyword due to the Haskell/SQL semantic confusion for the "where" keyword - we've had that discussion before, I just forgot about it last night when putting the PEP together) *Diff here: http://svn.python.org/view/peps/trunk/pep-3150.txt?r1=82992&r2=83002 Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On Tue, Jul 20, 2010 at 3:13 PM, Nick Coghlan <ncoghlan@gmail.com> wrote: <snip>
Nitpicking: Could the example code snippets please be made PEP 8 compliant (in particular, use 4-space indents)? They currently wobble between 2, 3, and 4-space indents, and the readability of Alex Light's example in particular is diminished by the use of only 2-space indents. Cheers, Chris

On Wed, Jul 21, 2010 at 6:13 AM, Alex Light <scialexlight@gmail.com> wrote:
No, the idea is for the indented suite to be a perfectly normal suite of Python code. We want to be able to define functions, classes, etc in there. Inventing a new mini-language specifically for these clauses would be a bad idea (and make them unnecessarily hard to understand) For the record, I've updated the PEP* based on the discussion in this thread (I switched to "given" as the draft keyword due to the Haskell/SQL semantic confusion for the "where" keyword - we've had that discussion before, I just forgot about it last night when putting the PEP together) *Diff here: http://svn.python.org/view/peps/trunk/pep-3150.txt?r1=82992&r2=83002 Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On Tue, Jul 20, 2010 at 3:13 PM, Nick Coghlan <ncoghlan@gmail.com> wrote: <snip>
Nitpicking: Could the example code snippets please be made PEP 8 compliant (in particular, use 4-space indents)? They currently wobble between 2, 3, and 4-space indents, and the readability of Alex Light's example in particular is diminished by the use of only 2-space indents. Cheers, Chris
participants (3)
-
Alex Light
-
Chris Rebert
-
Nick Coghlan