[Python-ideas] Documenting Python warts on Stack Overflow

Steven D'Aprano steve at pearwood.info
Wed Jan 2 03:00:49 CET 2013


On 02/01/13 10:46, Chris Angelico wrote:

> PHP's scoping rules are simpler than Python's. A variable inside a
> function is local unless it's explicitly declared global; function
> names are global. (That's not the whole set of rules, but close enough
> for this argument.) Python, on the other hand, adds the oddity that a
> name referenced inside a function is global unless, somewhere in that
> function, it's assigned to.


As given, comparing only treatment of locals and globals, I don't agree
that this makes PHP's scoping rules simpler.


PHP:
   if the name refers to a function:
     - the name is always global;
   otherwise:
     - the name is local unless explicitly declared global.

Python:
   if the name is declared global:
     - the name is always global;
   otherwise:
     - the name is global unless implicitly declared local.

(Implicitly local means "the name is bound to somewhere in the body of the
function".)

Of course, in reality Python includes further complexity: closures and
nonlocal, neither of which are available in PHP due to the lack of
local functions:

http://gadgetopia.com/post/4089


PHP is simpler because it does less.



-- 
Steven



More information about the Python-ideas mailing list