<div dir="ltr"><div>I am completely baffled by the behavior of this code with regards to the evaluation order of namespaces when assigning the class attributes.  Both classes are nested within a function I called whywhywhy.<br>
<br></div><div>I assumed that example1 and example2 classes would look first at their own namespace, then object, then the whywhywhy func namespace then global, and maybe module.  It seems this is not the case.<br></div><div>
<br>def whywhywhy(first, second, third):<br>    def print_stuff():<br>        print("func: first=", first)<br>        print("func: second=", second)<br>        print("func: third=", third)<br>
    print_stuff()<br><br>    class example1(object):<br>        print("1cls: first=", first)<br>        print("1cls: second=", second)<br>        print("1cls: third=", third) <br>        <br>
        second = second<br>        foo = third<br><br>    class example2(object):<br>        print("2cls: first=", first)<br>        print("2cls: second=", second)<br>        print("2cls: third=", third)<br>
<br>        second = second<br>        third = third<br><br>def second():<br>    pass<br><br>whywhywhy(1,2,3)<br><br><br></div>The code above produces the following output<br>"""<br><div>func: first= 1<br>func: second= 2<br>
func: third= 3<br>1cls: first= 1<br>1cls: second= <function second at 0xc6d380><br>1cls: third= 3<br>2cls: first= 1<br>2cls: second= <function second at 0xc6d380><br>Traceback (most recent call last):<br>  File "error.py", line 29, in <module><br>
    whywhywhy(1,2,3)<br>  File "error.py", line 18, in whywhywhy<br>    class example2(object):<br>  File "error.py", line 21, in example2<br>    print("2cls: third=", third)<br>NameError: name 'third' is not defined<br>
"""<br><br>In particular:<br><br>print_stuff behaves as I would expect<br>1cls: second #<--- Why does this look at the global namespace for second and not the whywhywhy func namespace first.<br>2cls: second #<--- Why can this no longer find third, it surely hasn't hit the line third=third <br>
<br></div><div>Thanks for any help you can provide. :)<br></div><div>Alastair<br></div></div>