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

Alex Light scialexlight at gmail.com
Wed Jul 21 19:19:20 CEST 2010


On Wed, Jul 21, 2010 at 12:02 PM, Jack Diederich <jackdied at gmail.com> wrote:

> <snip>

2a) No control flow statements in the block means if you need to
> augment the code to do a return/break/continue/yield you then have to
> refactor so everything in the "given:" block gets moved to the top and
> a 1-line change becomes a 10 line diff.
> 2b) Allowing control flow statements in the block would be even more
> confusing.
> 2c) Is this legal?
>     x = b given:
>       b = 0
>       for item in range(100):
>         b += item
>         if b > 10:
>           break


2a) you are missing the point of the given clause. you use it to assign
values to variables if, and only if, the only possible results of the
computation are
1) an exception is raised or
2) a value is returned which is set to the variable and used in the
expression no matter its value.
if there is the slightest chance that what you describe might
be necessary you would not put it in a "given" but do something like this:
(assumes that a given is applied to the previous statement in its current
block)

if some_bool_func(a):
    ans = some_func1(a, b, c)
else:
    ans = some_func2(a, b, c)
given: # note: given applied to the block starting with the "if" statement
    a = get_a()
    b = get_b()
    c = get_c()

2b) agreed but there is no reason for them to be disallowed except
readibility but they should be discouraged
2c) see it might be okay, depending on what people think of your second
question. in my opinion it should be illegal stylistically and
be required to be changed to

def summation(start, end)
    i = start
    while i < end:
        start += i
        i  += 1
    return start

def a_func():
    # do stuff
    x = b given:
        b = summation(0, 100)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100721/5b18fdf2/attachment.html>


More information about the Python-ideas mailing list