Andy Hayden's answer gives you the necessary fix, but I thought you might appreciate a little more background specifically directed at your question. Note that this describes the so-called "new" object model, though the old model (from pre-2.2 days, and still the default in Python 2, operates pretty much the same).
When the interpreter compiles a class is reads the whole class body and saves the definitions (in your case, someFunction
and __init__
) in a dictionary. It builds a list of "base classes" (classes from which it inherits), and then it calls the class's metaclass (unless you take special action, in Python your classes' metaclass will be the built-in type type
) with the class name, the list of base classes and the dict representing the class's namespace as arguments.
In Python 3, where the new model is the default, you can set the metaclass of a class using a keyword argument to the class
declaration. This allows you to verify my assertion. Consider the following program:
class print_args(type):
def __new__(cls, name, bases, name_space):
print("class:", cls)
print("Declaring class", name)
print("Bases:", bases)
print("Namespace:", name_space)
return type.__new__(cls, name, bases, name_space)
class A: pass
class B: pass
print("Defining C")
class C(A, B, metaclass=print_args):
CVar = "class attribute"
def myMethod(self, a):
return self.N, self.CVar
def __init__(self, N):
self.N = N
print("Creating c")
c = C(42)
print(c.CVar, C.CVar)
print(C.__dict__)
print(c.__dict__)
c.CVar = "instance attribute"
print(c.CVar, C.CVar)
print(c.__dict__)
The print_args
class is a simple metaclass, that mimics the action of type
by inheriting from it and delegating the real work to type.__new__
after a bit of printing. When the C
class is declared, it outputs
Defining C
class: <class '__main__.print_args'>
Declaring class C
Bases: (<class '__main__.A'>, <class '__main__.B'>)
Namespace: {'__qualname__': 'C', '__module__': '__main__', '__init__': <function C.__init__ at 0x10778bbf8>, 'myMethod': <function C.myMethod at 0x10778bc80>, 'CVar': 'class attribute'}
Note that this all happens when the C class is declared - you can tell this by the next lines of output. Note also the values printed tell you what was passed to type.__new__
. The program next creates a C
instance, and demonstrates that the class attribute can also be accessed as an instance attribute - method resolution order is applied to attribute lookups too - in fact the instance could have also accessed attributes of the A
and B
classes had there been any.
Creating c
class attribute class attribute
At this point the __dict__
of class C
looks like this:
{'CVar': 'class attribute',
'__module__': '__main__',
'__doc__': None,
'__init__': <function C.__init__ at 0x10778bbf8>,
'myMethod': <function C.myMethod at 0x10778bc80>}
and the dict of its instance looks like this:
{'N': 42}
It contains only the instance attribute N
. But after a value is bound to c.CVar
we can see that the instance attribute now differs from the class attribute.
instance attribute class attribute
This is confirmed by an updated look at the instance's __dict__
.
{'CVar': 'instance attribute', 'N': 42}
Hopefully this will give you a bit more insight into what happens when you declare classes, and how the instances and the classes are related.