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

Steven D'Aprano steve at pearwood.info
Fri Jul 17 04:57:11 CEST 2009


On Fri, 17 Jul 2009 03:58:21 am Chris Perkins wrote:

> > In either case, writing the sums-and-squares before the return
> > suggests that these are common code best written as re-usable
> > functions. But "where" changes the focus of the coder away from
> > factoring code *out* of functions into placing code *inside*
> > internal blocks. It encourages the coder to think of sum_of_squares
> > as a private implementation detail instead of a re-usable
> > component. And that is, I believe, harmful.
>
> But surely you agree that _sometimes_ there is code that really is an
> implementation detail - not everything is reusable, or at least not
> usefully reusable. 

Of course. What your suggestion gives us, though, is a second way of 
including multi-line functions:

def parrot():
    x = 1
    y = 2
    return x + y


versus


def parrot():
    return x + y where:
        x = 1
        y = 2

I'm not convinced that allowing the second is either necessary or 
beneficial.


> Taking your argument to the extreme, every 
> function would be only one line of code. I prefer to trust
> programmers to make the best use of the tools available to create
> good code.

The Python community takes the attitude that we're all consenting adults 
here, and that if you want to shoot yourself in the foot, you should be 
allowed to. And that's fine. But it's only fine because Python, by and 
large, is a safe language, one with very few jagged edges, which 
encourages good practices rather than bad.

Which brings me to this:

> In other words:
>
> Jan Kaliszewski:
> >Every language feature can be misused. More powerful feature --
> > larger possibilities of misusing. But language is not for
> > substituting programmer's sense.
>
> Yeah, ditto to what he said.

Some language features are begging to be misused, and although I've 
occasionally wanted a way to define code blocks on the fly outside of a 
function, I'm leaning towards the conclusion that this suggestion is a 
feature where the abuses outweigh the benefits.



-- 
Steven D'Aprano



More information about the Python-ideas mailing list