[Tutor] Why thus?

Christian Tismer tismer@tismer.com
Sun, 12 Mar 2000 22:29:57 +0100


Ok, let's see .-)

> Arthur Siegel wrote:
> 
> Can't quite see what's happening here.
> 
> counter=0
yes, you made a global.

> class Count:
>  def __init__(self):
>     print counter
> Count()

Ok, counter is not defined in Count, and it is
correctly identified as a global.

> counter=0

> class Count:
>  def __init__(self):
>   counter=counter+1
>   print counter
> Count()

The difference is: When compiling your code, the compiler
tries to figure out what is global and what not. Since
counter is assigned to, it becomes a local. But the object
does not yet exist when you request it:
   counter=counter+1
wants to retrieve the current value, and it isn't set yet.
You are accessing a local variable which already has its
slot and is ready to receive a value, but cannot until
it is initialized.

cheers - chris

-- 
Christian Tismer             :^)   <mailto:tismer@appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home