0

so I am trying to create many instances of the same class in python and store them in a list and use them as necessary. This is due to the fact that in the eventual script I will use this in I need a variable number of instances of this class.

So my code is:

class Node():
    def __init__(self,index):
        self.index = index

    def printer(self):
        print(self.index)


classlist = []

for i in range(100):
    classlist.append(Node(i))

for i in classlist:
    print(i.printer())

So I get the correct printed index output but I keep also getting None

so the output is:

0
None
1
None
2
None
3
None
4
None
5
None

What am I doing that causes the None to be printed? how can I stop this?

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
Edwin
  • 31
  • 2
  • 7
  • Your `printer()` function doesn't return anything, I'm guessing you meant to do `return self.index` in the printer function. But it is more convenient to simply not have a printer function and just `print(i.index)`. This seems like a x and y issue – aaa Jul 28 '20 at 19:46
  • oh my god you are right. I am sorry that was a very silly mistake. – Edwin Jul 28 '20 at 19:54

0 Answers0