python3: 'where' keyword

Paul Rubin http
Sat Jan 8 01:30:32 EST 2005


Nick Coghlan <ncoghlan at iinet.net.au> writes:
> > Usage could be something like:
> >  >>> res = [ f(i) for i in objects ] where:
> >  >>>     def f(x):
> >  >>>         #do something
> 
> Hmm, this is actually a really interesting idea. Avoiding accidental
> namespace conflicts is certainly one of the advantages of using lambdas.

I like it too.  Seems a little perl-ish, but what the hey.

> In fact, any subexpressions in a complicated expression can be
> extracted and named for clarity without fear of screwing up the
> containing namespace, which would be an enormous boon for software
> maintainers.

Sure, why not:

    x = (-b + sqrt(discriminant))/(2*a) where:
          discriminant = b*b - 4*a*c

Maybe you could just have a where: suite that binds variables
within the suite:

    where:
       def f(x):
          #do something
    res = [ f(i) for i in objects ]

    where:
       discriminant = b*b - 4*a*c
    x = (-b + sqrt(discriminant))/(2*a) 

Syntax is
   where:
      suite
   statement

the suite has its own scope so any variable created there is local to
the suite plus the following statement.  The scope vanishes after the
statement.



More information about the Python-list mailing list