[Tutor] Namespace Problem

Carroll, Barry Barry.Carroll at psc.com
Tue Nov 8 03:28:19 CET 2005


Kent:

Thank you for the explanation.  This is a subtlety of Python that has
tripped me up before.  

Barry

> Date: Mon, 07 Nov 2005 16:26:39 -0500
> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] Namespace Problem
> Cc: "'tutor at python.org'" <tutor at python.org>
> Message-ID: <436FC68F.9020806 at tds.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 

<<snip>>

> >         seq += 1
> 
> The above statement binds a value to the name 'seq'. If a name is bound
> anywhere within a block,  the compiler treats it as a local name and it
> will not be looked up in the global namespace. When the struct.pack() line
> is executed, seq is not bound in the local namespace and you get an error.
> The solution is to include 'global seq' in do_stuff(); this tells the
> compiler that seq should always be treated as a global name even though it
> is bound in the current block.
> 
> This a little bit subtle because you are using seq += 1. Still this is
> binding a new value to seq and you need to declare it global.
> 
> Kent
> 

<<snip>>




More information about the Tutor mailing list