[Python-ideas] Where-statement (Proposal for function expressions)

Jan Kaliszewski zuo at chopin.edu.pl
Wed Jul 15 19:21:19 CEST 2009


Hello,

15-07-2009, 09:55 Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:

> One such extension might be a "where" block. Applied
> to the current problem:
>
>    foo(f) where:
>      def f(x):
>        ...

At first glance, +1 from me.

15-07-2009, 12:25 Paul Moore <p.f.moore at gmail.com> wrote:

> inside the [...] - which clearly violates the "no indented block
> structure within expressions" design rule.
>
> My initial instinct was that the original form violated that rule, too
> - but if you interpret the where block as a statement postfix, you're
> (almost) OK. The (almost) bit is because the where block has to be
> limited to simple (single-line) statements.

> stmt where:
>    a
>    b
>    c
> =>
>
> a
> b
> c
> stmt
>
> with the proviso that variables defined in a,b,c are only available
> within a, b, c and stmt.

I don't like the direction of limiting content of the block to set of  
singular simple statements. I suppose where-block should simply create a  
separate nested scope.

So...

> x = 1
> y = x where:
>     x = 2
> print x
>
> What does that print?

I tkink it should print 1 -- because, if where-block creates a separate  
nested scope, its local variables shouldn't leak.

But there is another question: what is the value of y: 1 or 2? Or, more  
genarally: should be the line ended with 'where' included *in* that nested  
scope or not? I suppose it should, so the final value of y should be 2.

Another example:

a = 7
b = 8
x = 'X'
z = 'zzzzz'

foo(a, b, c("It's..."), x) where:
     # a = input() in Python 3.x :)
     a = raw_input(z)  # -> zzzzz
     try:
         a = int(a)
     except ValueError:
         a = 0
         def b(bar):
             'very complex function'
             print(a, bar)
     else:
         def b(bar):
             'also very complex function'
             print(bar, a, bar)

     def c(*args, **kwargs):
         'also also very complex function'

print(a, b)  # -> 7 8


Best regards,

-- 
Jan Kaliszewski <zuo at chopin.edu.pl>



More information about the Python-ideas mailing list