I can’t figure out what’s wrong with this very simple snippet:
class A(object):
def printme(self):
print "A"
self.printme()
a = A()
Error output:
Traceback (most recent call last): File "prog.py", line 1, in
<module>
class A(object): File "prog.py", line 5, in A
self.printme() NameError: name 'self' is not defined
The following should explain the problem. Maybe you will want to try this?
The name
selfis only defined inside methods that explicitly declare a parameter calledself. It is not defined at the class scope.The class scope is executed only once, at class definition time. “Calling” the class with
A()calls it’s constructor__init__()instead. So maybe you actually want this: