"None" in array - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: "None" in array (/thread-17102.html) |
"None" in array - dl0banter - Mar-28-2019 I am getting None in an array as output with the following code. Basically there are 3 items in the list which I can print as only three items. When the iterator is doing the draw from each object, output from the program in between each one is a "None" for some reason. class Shape: def __init__(self): self.name = "Shape" def draw(self): raise NotImplementedError("Please Implement this method") class Rectangle(Shape): def __init__(self): self.name = "Rectangle" def draw(self): print("This object is a rectangle.") class Circle(Shape): def __init__(self): self.name = "Circle" def draw(self): print("This object is a circle.") class Square(Shape): def __init__(self): self.name = "Square" def draw(self): print("This object is a square.") if __name__ == "__main__": shapes = [] inst = Rectangle() shapes.append(inst) inst = Circle() shapes.append(inst) inst = Square() shapes.append(inst) print(len(shapes)) print(shapes) for i in shapes: print(i.draw())
RE: "None" in array - Yoriz - Mar-28-2019 This is because for i in shapes: print(i.draw())the print here is printing the result of the method draw and as there is no return value it defaults to None remove the print and it will no longer print None. for i in shapes: i.draw() |