Replacement for keyword 'global' good idea? (e.g. 'modulescope' or 'module' better?)

Antoon Pardon apardon at forel.vub.ac.be
Tue Aug 16 11:27:10 CEST 2005


Op 2005-08-06, Mike Meyer schreef <mwm at mired.org>:
> "John Roth" <newsgroups at jhrothjr.com> writes:
>> <seberino at spawar.navy.mil> wrote in message
>> news:1123270841.195692.21470 at g47g2000cwa.googlegroups.com...
>> A much better idea would be to fix the underlying
>> situation that makes the global statement necessary.
>
> You can't "fix" this. This code (in some python-like langauge that
> isn't python):
>
> x = 23
>
> def fun():
>     x = 25
>     # Rest of code
>
> has two possible interpretations.
>
> Either the occurrence of x in fun references the global, or it
> references a local that shadows the global. There are reasons for
> wanting both behaviors. So you have to have some way to distinguish
> between the two, and you want it to happen per variable, not per
> function. The method with the fewest keywords is to have one be the
> default, and some keyword that triggers the other.
>
> So the only way to remove the global statement would be to have some
> way to mark the other interpretation, with say a "local"
> decleration. I thik that would be much worse than "global". For one
> thing, most variables would be local whether or not they are
> declared. Second, having an indication that you need to check module
> globals in the function is a better than not having that clue there.

I disagree here. The problem with "global", at least how it is
implemented in python, is that you only have access to module
scope and not to intermediate scopes.

I also think there is another possibility. Use a symbol to mark
the previous scope. e.g. x would be the variable in local scope.
@.x would be the variable one scope up. @. at .x would be the
variable two scopes up etc.

-- 
Antoon Pardon



More information about the Python-list mailing list