Namespacedifference reading - writing ??

Peter Abel p-abel at t-online.de
Fri Jun 27 14:55:31 CEST 2003


Can anybody tell me if the following 2 examples are OK
as shown below? In my opinion it is **not**.

1)
>>> def outer():
... 	x=1
... 	def inner():
... 		print 'x in inner    =',x
... 		#x=100
... 		#print 'x set in inner=',x
... 	inner()
... 	print 'x in outer    =',x
... 
>>> outer()
x in inner    = 1
x in outer    = 1


2)
>>> def outer():
... 	x=1
... 	def inner():
... 		print 'x in inner    =',x
... 		x=100
... 		print 'x set in inner=',x
... 	inner()
... 	print 'x in outer    =',x
... 
>>> outer()
x in inner    =Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "<interactive input>", line 7, in outer
  File "<interactive input>", line 4, in inner
UnboundLocalError: local variable 'x' referenced before assignment

Is there really a difference in namespacehandling depending
on writing or reading variables?

Regards
Peter




More information about the Python-list mailing list