I am confused about instance and class variables in python. I am confused about how this code:
class A:
def _init__(self):
print "initializer called"
#l is an instance variable
self.l = []
print len(l)
def printList(self):
print len(self.l)
a = A()
print "here"
a.printList()
produces the following output:
here
Traceback (most recent call last):
File "C:\Python27\a.py", line 16, in <module>
a.printList()
File "C:\Python27\a.py", line 10, in printList
print len(self.l)
AttributeError: A instance has no attribute 'l'
Is the initializer not being called? Why does "here" print, but the print statement in the initializer doesn't? Should 'l' be a class variable? I thought making it an instance variable using 'self' would be sufficient, is that not correct? Am I incorrect about the scope of these variables?
I have looked at these sources, but they didn't help:
Python: instance has no attribute
Python: Difference between class and instance attributes