Re: baffled classes within a function namespace. Evaluation order.

Discussion in 'Python' started by Alastair Thompson, Apr 25, 2013.

  1. Thats a good pointer to what is going on. Thank you Bas.

    I am familiar with error such as

    x=1
    def foo():
    x = 2
    def erm():
    print(x)
    x=3
    erm()
    foo()
    UnboundLocalError: local variable 'x' referenced before assignment.

    It seems a bit different for classes (below), as it jumps out to get the
    value from the global name space, where it didn't for functions (above).

    x=1
    def foo():
    x = 2
    class erm():
    print(x)
    x = 3
    foo() # This evaluates == 1

    But you certainly have explained why "NameError: name 'third' is not
    defined" occurs.
    Alastair Thompson, Apr 25, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    2
    Views:
    390
    Ilias Lazaridis
    Apr 24, 2005
  2. Alfonso Morra

    Baffled by this ... (casting of function pointers)

    Alfonso Morra, Aug 26, 2005, in forum: C Programming
    Replies:
    7
    Views:
    366
    Denis Kasak
    Aug 26, 2005
  3. Ilias Lazaridis
    Replies:
    74
    Views:
    752
    Ilias Lazaridis
    Apr 4, 2005
  4. Alastair Thompson
    Replies:
    1
    Views:
    150
  5. Peter Otten
    Replies:
    3
    Views:
    130
    Steven D'Aprano
    Apr 27, 2013
Loading...

Share This Page