[Python-ideas] Statement local functions and classes (aka PEP 3150 is dead, say 'Hi!' to PEP 403)

Nick Coghlan ncoghlan at gmail.com
Fri Oct 14 05:38:32 CEST 2011

On Fri, Oct 14, 2011 at 1:07 PM, Carl M. Johnson
<cmjohnson.mailinglist at gmail.com> wrote:
> To me the limitations of 403 are its strength. I don't want to see people doing crazy inside-out code.

This has long been my fear with PEP 3150, but I'm beginning to wonder
if that fear is overblown.

> Let's say we had a keyword OoO for "out of order":
> OoO result = flange(thingie):
>        OoO thing = doohickie(majiger):
>                part_a = source / 2
>                part_b = source ** 2
>                majiger = blender(part_a, part_b)

But now consider this:

    part_a = source / 2
    part_b = source ** 2
    majiger = blender(part_a, part_b)
    thingie = doohickie(majiger)
    result = flange(thingie)

And now with a couple of operations factored out into functions:

    def make_majiger(source):
        part_a = source / 2
        part_b = source ** 2
        return blender(part_a, part_b)

    def make_thingie(source):
        return doohickie(make_majigger(source))

    result = flange(make_thingie(source))

All PEP 3150 is allowing you to do is indent stuff that could
potentially be factored out into a function at some point, without
forcing you to factor it out *right now*:

    result = flange(thingie) given:
        thingie = doohickie(majiger) given:
            part_a = source / 2
            part_b = source ** 2
            majiger = blender(part_a, part_b)

So is the "inline vs given statement" question really any more scary
than the "should I factor this out into a function" question?

I expect the style guidelines will boil down to:
- if you're writing a sequential process "first we do A, then we do B,
then we do C, etc", use normal inline code
- if you're factoring out subexpressions from a single step in an
algorithm, use a given statement
- as a general rule, given statements should be used for code that
could reasonably be factored out into its own function


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list