[Python-ideas] Documenting Python warts on Stack Overflow

Chris Angelico rosuav at gmail.com
Wed Jan 2 05:35:39 CET 2013


On Wed, Jan 2, 2013 at 1:00 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> 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;

Not quite. Python has the concept of "names" which might be bound to
function objects, or might be bound to simple integers. PHP has two
completely separate namespaces.

<?php
function foo()
{
        echo "Function foo\n";
}
$foo = 1;
echo "foo: ".$foo."\n";
foo();
?>

The variable $foo and the function foo() don't collide, so this isn't
a rule that governs where the name "foo" is looked up. Python has no
such distinction, so code like this does exactly what you would
expect:

def foo():
  pass
bar = foo
def quux():
  bar() # No assignment in the function, so look for a global name 'bar'.


> PHP is simpler because it does less.

Right. And the rules of a Turing tarpit like Ook are even simpler.
Further proof that design warts are not, in and of themselves,
necessarily bad.

ChrisA



More information about the Python-ideas mailing list