1

Based on the following example how come I can print the var xyz from within a class as I always thought the class was stored in its own namespace.

xyz = 123
>>> class test:
...    def __init__(self):
...        pass
...    def example(self):
...        print xyz
...
>>> test().example()
123

Thanks

3 Answers 3

3

Classes are not stored in their own namespace, instead they define their own namespace, i.e variables defined inside a class are local to it and can't be accessed directly outside of it. But, a class can access variables from global scope if required.

Python uses LEGB rule to find the value of a variable:

L: Local scope

E: Enclosing scope

G: Glocal scope

B: Built-ins


Example of LEGB:

z = 3
def enclosing():
    y = 2
    def local_func():
        x = 1
        print x        #fetch this from local scope
        print y        #fetch this from enclosing scope
        print z        #fetch this from global scope
        print sum      #fetch this from builtins
    local_func()
enclosing()    

output:

1
2
3
<built-in function sum>

So, in your case as xyz is not found in both local and enclosing scope, so global scope is used to fetch the value.

Related to scopes:

Why am I getting an UnboundLocalError when the variable has a value?

What are the rules for local and global variables in Python?

Sign up to request clarification or add additional context in comments.

1 Comment

Great explanation. The details are a bit more complicated (because Python decides whether to access a local, a closure cell, or a global at function compilation time, not run time), but the OP shouldn't have to worry about any of that (unless he's doing something unusual, like execing new code or modifying locals behind the compiler's back).
1

Classes are not stored in their own namespace. They are stored in the namespace they are defined in; in your case, that's the global namespace of the interpreter session.

Methods within classes can still refer to names in that same namespace.

Perhaps you were confused with the scope at class definition time. The class body is executed as if it is a function; the local namespace of that function then forms the attributes of the resulting class. You can still reach names from outer scopes from there, however:

foo = 'bar'

class Spam(object):
    baz = foo   # works, foo can be referenced

    def ham(self):
        xyz = foo  # works, foo is a global and can be referenced
        xyz = baz  # does **not** work, baz is part of the class now.
        xyz = self.baz or Spam.baz   # these do work

Comments

0

The class is in a namespace, but that is besides the point. Your question is more about accessibility of variables/members. The class's members are encapsulated, meaning that access is through the class.

xyz is in global namespace. This means that the variable is accessible throughout without going through a class. This is a thing to avoid. Classes are easy to create (both in defining them and instantiating them).

A namespace is easier to understand as simply being the name that is pre-pended to the classes contained within that namespace. Thought of this way, much more remains class-focused, a primary goal of OOP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.