The short version is: You want k.Adlist
here, not Adlist
. It's similar to the fact that when you want to access an attribute, you need self.spam
, not just spam
, although the details are different.
The medium-long version is:
While a class body does create a namespace, it's not like a function's body.
In particular, the locals in a class body cannot be captured by classes or functions defined within the class body, the way function locals can be captured by classes r functions defined within the function body.
However, when the class
statement is executed, the namespace of that class body becomes the set of attributes of the class object. So, you can access those variables through the class (or through any of its instances, but that isn't relevant here).
Meanwhile, there are multiple other problems with your code:
__main__
isn't defined anywhere. You probably meant '__main__'
?
- The
array
that you create in Graph.__init__
is just a local variable, which goes away as soon as __init__
returns, so nobody can ever access it again. You probably wanted self.array
here.
- The
head
in Adlist.__init__
has the same problem.
- The
printgraph
function doesn't take a self
parameter, so it can't be called as a method on a k
instance.
- The
printgraph
function tries to access something named array
, but there's nowhere it could get such a thing from. Sure, instances of the class Graph
(if you fix the first problem) have an array
attribute, but there's no connection between a k
instance and any Graph
instance. As far as that k
object knows, there could be 300 Graph
s, or none at all. Maybe you wanted this to be a method of Graph
, not of k
?
l.Graph(5)
creates a Graph
instance and then immediately discards it. That isn't very useful.
- Calling
l.Graph
instead of k.Graph
, while legal, is potentially a bit misleading—it implies to the reader that there's some kind of connection between the specific instance l
and the Graph
, but there really isn't.
Putting it all together:
class k:
class Graph:
def __init__(self, v):
self.array = [k.Adlist() for i in range(v)]
def printgraph(self):
for i in self.array:
print(i.head)
class Adlist:
def __init__(self):
self.head = []
if __name__ == '__main__':
g = k.Graph(5)
g.printgraph()
Of course this will just print five empty lists, but that's better than a NameError
and another NameError
when you fix that and so on…