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

Daniel Stutzbach daniel at stutzbachenterprises.com
Sat Jul 18 14:45:44 CEST 2009


On Fri, Jul 17, 2009 at 7:39 PM, Steven D'Aprano <steve at pearwood.info>wrote:

> In a real sense, the proposed 'where' block breaks the flow of reading
> code. Normally, reading a function proceeds in an orderly fashion from
> the start of the function to the end in (mostly) sequential order, a
> bottom-up process:
>

Normally, that's true, but that's not true when defining a sub-function.
Consider the following:

def foo(items):
    def compute_sort_value(item):
        # compute value based on item's properties
        return value
   # a bunch of other code is here
    items.sort(compute_sort_value)

Above, the body of compute_sort_value appears long before it is executed.
Now consider:

def foo(items):
    # a bunch of other code is here
    items.sort(key=mean) where:
        def compute_sort_value(item):
            return value

Now, the body of compute_sort_value appears exactly where it is executed:
within the call to items.sort().

--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090718/0eb29417/attachment.html>


More information about the Python-ideas mailing list