Namespaces in functions vs classes

Discussion in 'Python' started by Gerald Britton, Apr 17, 2011.

  1. I apologize if this has been answered before or if it is easy to find
    in the docs. (I couldn't find it but might have missed it)

    I'm trying to understand the differences between namespaces in class
    definitions vs. function definitions. Consider this function:

    >>> def a():

    .... foo = 'foo'
    .... def g(x):
    .... return foo
    .... print g(1)
    ....
    >>> a()

    foo
    >>>


    Now, I replace the first "def" with "class":

    >>> class a():

    .... foo = 'foo'
    .... def g(x):
    .... return foo
    .... print g(1)
    ....
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 5, in a
    File "<stdin>", line 4, in g
    NameError: global name 'foo' is not defined

    So, the variable "foo" is not available inside the function inside
    class as it is inside the (inner) function. I figured this was just
    because the class was still being defined, so I tried this:

    >>> class a():

    .... foo = 'foo'
    .... def g(x):
    .... return foo
    ....
    >>> x = a()
    >>> x.g()

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 4, in g
    NameError: global name 'foo' is not defined
    >>>


    which still fails. I had expected that "foo" would be available
    within the namespace of the object instance. I was wrong. For my
    final attempt, I add the prefix "a." to my use of "foo"

    >>> class a():

    .... foo = 'foo'
    .... def g(x):
    .... return a.foo
    ....
    >>> x = a()
    >>> x.g()

    'foo'

    So, this works and I can use it. However, I would like a deeper
    understanding of why I cannot use "foo" as an unqualified variable
    inside the method in the class. If Python allowed such a thing, what
    problems would that cause?

    --
    Gerald Britton
     
    Gerald Britton, Apr 17, 2011
    #1
    1. Advertisements

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. Craig Deelsnyder

    Best Practices - solution - namespaces - classes

    Craig Deelsnyder, Aug 3, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    531
    Vincent V
    Aug 4, 2003
  2. ravi sankar
    Replies:
    2
    Views:
    517
    abdul bari
    Aug 27, 2003
  3. Replies:
    3
    Views:
    741
    Flinky Wisty Pomm
    Apr 3, 2006
  4. Razvan
    Replies:
    11
    Views:
    1,117
    Andrew Thompson
    Jul 17, 2004
  5. Bernd Fuhrmann
    Replies:
    8
    Views:
    477
    Rob Williscroft
    Dec 28, 2003
  6. Xiangliang Meng
    Replies:
    1
    Views:
    1,918
    Victor Bazarov
    Jun 21, 2004
  7. Replies:
    7
    Views:
    760
    Alex Hunsley
    Aug 12, 2006
  8. mojmir
    Replies:
    3
    Views:
    456
    mojmir
    Sep 12, 2008
Loading...