News for hackers. Stuff that matters.
Re:Anti-Patterns in Python Programming by syllogism on Wednesday July 09, 2014 @05:08PM (#8012247)
In Python 3 there's the nonlocal keyword to deal with the scoping thing.

The default arguments thing is worse than a lot of the stuff Python 3 corrected.

Re:Anti-Patterns in Python Programming by dalke on Wednesday July 09, 2014 @10:54PM (#8013337)
I don't see how nonlocal could fix this. Could you explain?

More specifically, given a better 'default arguments thing', how would you interpret:

    x = [2]
    def f(x=x*5):
        x.append(4)
With the earlier conversion it's:

    x = [2]
    def f(x=DefaultArg):
        if x is DefaultArg:
            x = x*5
        x.append(4)
This isn't going to work because the x inside of f() is different than the outside x, and you'll get the error message I mentioned.

If you add a nonlocal, as in:

    x = [2]
    def f(x=DefaultArg):
        nonlocal x
        if x is DefaultArg:
            x = x*5
        x.append(4)
then you'll get "SyntaxError: name 'x' is parameter and nonlocal".

What other solution are you thinking of?

Re:Anti-Patterns in Python Programming by syllogism on Friday July 11, 2014 @03:44PM (#8022012)
Ah I see. Yeah nonlocal can't fix that.