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

Steven D'Aprano steve at pearwood.info
Mon Oct 17 02:30:55 CEST 2011


Nick Coghlan wrote:
> On Sun, Oct 16, 2011 at 9:01 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> If it's not tested, how do you know it does what you need?
> 
> Because my script does what I want. Not every Python script is a
> production application. We lose sight of that point at our peril.
> 
> Systems administrators, lawyers, financial analysts, mathematicians,
> scientists... they're not writing continuously integrated production
> code, they're not writing reusable modules, they're writing scripts to
> *get things done*.

"And it doesn't matter whether it's done correctly, so long as SOMETHING 
gets done!!!" <wink>

I'm not convinced that requiring coders to write:

given a, b c
do f(a, b, c)

instead of

do f(a, b, c)
given a, b, c

gets in the way of getting things done. I argue that the second form 
encourages bad practices and we shouldn't add more features to Python 
that encourage such bad practices. I certainly don't think that people 
should be forced to test their code if they don't want to, and even if I 
did, it wouldn't be practical.


[...]
> All that said... is your objection *only* to the "statement local"
> part of the proposal? That part is actually less interesting to me
> than the out of order execution part. However, our past experience
> with list comprehensions suggests that exposing the names bound inside
> the suite in the containing scope would be confusing in its own way.

No. Introducing a new scope is fine. Namespaces are cool, and we should 
have more of them. It's the out of order part that I dislike. Consider this:

a = 1
b = 2
c = 3
result = some_function(a, b, c) given:
     d = "this isn't actually used"
     a = c
     b = 42


I see the call to some_function, I *think* I know what arguments it 
takes, because a b c are already defined, right? But then I see a 
"given" statement and the start of a new scope, and I have that moment 
of existential dread where I'm no longer sure of *anything*, any of a b 
c AND some_function could be replaced, and I won't know which until 
after reading the entire given block. At which point I can mentally 
back-track and finally understand the "result =" line.

If a, b aren't previously defined, then I have a mental page fault 
earlier: "what the hell are a and b, oh wait, here comes a given 
statement, *perhaps* they're defined inside it...".

Either way, out of order execution hurts readability. There's a reason 
that even mathematicians usually define terms before using them.

But for what it's worth, if we end up with this feature, I agree that it 
should introduce a new scope, and the "given" syntax is the nicest I've 
yet seen.



-- 
Steven




More information about the Python-ideas mailing list