scope question

Mark Robinson m.1.robinson at herts.ac.uk
Tue Aug 7 16:59:27 CEST 2001


Can anyone explain the following behaviour to me

if I type:

count = 0
def function():
	print count

function()


that works fine, however, if I type this:

count = 0
def function():
	count += 1
	print count

function()

I get the following exception

UnboundLocalError: local variable 'count' referenced before assignment

why does it find the global variable in the first case and not in the 
second?

blobby




More information about the Python-list mailing list