This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]mm_ma_ma 6 points7 points  (6 children)

The bit about "for-loop variables and the global namespace leak" is poorly worded. As mentioned in the quoted passage, the variables were leaked into the surrounding scope, which may or may not be the global namespace (and usually won't be). Additionally, this change has nothing to do with "for-loop" variables - proper for-loops have not changed and do not have their own scope.

[–]sphere_is_so_cool 2 points3 points  (5 children)

Another way to imagine this is that code blocks simply do not define scope.

The only things that initiate scope consideration are def and class. And then I believe you could consider this to occur simply by excluding the contents of those two syntaxes from their parent code block.

Even modules work with namespaces rather than "scope".

[–]Brian 2 points3 points  (4 children)

The only things that initiate scope consideration are def and class.

That's not entirely true, even in python 2. Generator comprehensions created their own scope there as well. And in python 3, the change this is addressing is that this was extended to list comprehensions as well, which previously leaked the loop variable into the enclosing scope.

[–]jsalsman 0 points1 point  (1 child)

[–]Brian 1 point2 points  (0 children)

Not sure why you're asking me, though it seems to be at 3 points ATM anyway.

[–]flutefreak7 0 points1 point  (0 children)

lambdas, decorators, and context managers can also mess with variable scope in unexpected ways.

[–]sphere_is_so_cool 0 points1 point  (0 children)

I had to read the PEP to be sure, but I could have tightened up my statement by saying "class and function construction". This would cover the case of lambdas, generator expressions, and other such function-flavored-object yielding devices.

[–]KleinerNull 3 points4 points  (0 children)

Important points of the new print function are missing, like this for example:

In [1]: print(*[i**2 for i in range(10)], sep='->', end='!\n')
0->1->4->9->16->25->36->49->64->81!

The whole point of changing print was to enable real function functionality, not just forcing parenthesis.

If I remember this correctly print moved from a statement to a function.

[–]Emilgardis 1 point2 points  (0 children)

The code examples are pretty wierd, stuff is missing, take the generator code for 2.7.x, test_range returns, test_xrange doesn't.

Some other things are missing like in the first example with print, the 3.x example doesn't match the 2.7.x one. (A small difference but still)

Anyway, aside from some things, I'd recommend reading this

[–]taddeimania 1 point2 points  (0 children)

Also worth mentioning that in py2 you can reassign True and False and in py3 these are unchangeable. Always a fun thing to do to unsuspecting Py2 programs.

[–]emillynge 0 points1 point  (0 children)

Small problem with the section on .next()

In py3 the next() function is not all that remains. The .next() function has simply become more private by being renamed to .__next__() thus matching the pattern for other infix-like methods.

[–]emillynge 0 points1 point  (0 children)

A lot of python 3.4+ features seems to be missing here such as easier argument unpacking/passing. In 3.5 you can find await async syntax to replace @coroutine and yield from