the global keyword:

Rick Johnson rantingrickjohnson at gmail.com
Tue Jun 21 19:43:00 EDT 2016


On Tuesday, June 21, 2016 at 5:32:43 PM UTC-5, Chris Angelico wrote:
> On Wed, Jun 22, 2016 at 8:20 AM, Rick Johnson:
> > Then one could have added module-level symbols without all
> > the semantic hubbub.
> >
> >     MSFL.foo = 0
> >
> >     def iter_foo():
> >         MSFL.foo += 1
> >
> 
> And don't forget that you would need to call this function as
> MSFL.iter_foo(). Module-level functions are globals too.

That's true, you got me :-). A better implementation would
be to only require the "resolution to module level" from
*WITHIN* foreign scopes. Which would remove the redundancy of
the top-level assignment/access. 

Here is a new version

    modglo = 0
    def incr_modglo():
        MSFL.modglo += 1
    incr_modglo()
    print modglo

...as opposed to the neo-classical Python way of:

    modglo = 0
    def incr_modglo():
        global modglo
        modglo += 1
    incr_modglo()
    print modglo

...which is implementing an antiquated quasi-state-machine
pattern. Yuck!


More information about the Python-list mailing list