Scoping rules for class definitions

Rotwang sg552 at hotmail.co.uk
Fri Apr 4 14:37:56 EDT 2014


Hi all. I thought I had a pretty good grasp of Python's scoping rules, 
but today I noticed something that I don't understand. Can anyone 
explain to me why this happens?

 >>> x = 'global'
 >>> def f1():
     x = 'local'
     class C:
         y = x
     return C.y

 >>> def f2():
     x = 'local'
     class C:
         x = x
     return C.x

 >>> f1()
'local'
 >>> f2()
'global'



More information about the Python-list mailing list