felix001
felix001

Reputation: 16691

Variables, namespace and classes

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

Upvotes: 1

Views: 200

Answers (3)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250891

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?

Upvotes: 3

paegun
paegun

Reputation: 715

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.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121484

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

Upvotes: 1

Related Questions