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

Carl M. Johnson cmjohnson.mailinglist at gmail.com
Fri Oct 14 05:07:43 CEST 2011

On Oct 13, 2011, at 2:01 PM, Nick Coghlan wrote:

> On Fri, Oct 14, 2011 at 7:01 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>> Withdrawing PEP 3150 altogether seems like an over-
>> reaction to me. A lot of its problems would go away
>> if the idea of trying to make the names local to the
>> suite were dropped. That part doesn't seem particularly
>> important to me -- we manage to live without the
>> for-loop putting its variable into a local scope,
>> even though it would be tidier if it did.
> So, keep the PEP 3150 syntax, but don't make the inner suite special
> aside from the out of order execution?

To me the limitations of 403 are its strength. I don't want to see people doing crazy inside-out code. 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)

This violates the spirit of One Obvious Way from my perspective. If you have an OoO keyword, for every sequence of code you end up asking yourself, "Hmm, would this make more sense if I did it backwards or forwards?…" That will lead to a bunch of style guide wars and in many cases result, worse readability.

By comparison, PEP 403 allows you to do one thing (or two if classes are allowed). You can place a callback underneath the receiver of the callback. That's more or less it. The answer to "when should I use PEP 403?" is very clear: "When you're never going to pass a callback function to anything other than the one receiver." In the same way, "When do I use an @decorator?" has a clear answer: "When you're never going to want to use the undecorated form."

I can imagine a little bit of tweaking around the edges for this (see below), but otherwise, I think the core theory of PEP 403 is just right.

Slight tweak:

postdef items = sorted(items, key=@keyfunc(item)):
	item = item.lower()
	item = item.replace(" ", "")
	return item

In this case "keyfunc" is the name of the function (not that anyone will ever see it) and (item) is the argument list. I'm not convinced this tweak is better than having a full def on the line below, but it's worth considering.

More information about the Python-ideas mailing list