0

I created a class whith a method that print out one of it's fields:

class Rule:
def __init__ (self,number, callout):
    self.number=number
    self.callout=callout

def shout(self):
    print(self.callout)

I then create a list of these objects to iterate over, calling shout() for each of them:

Fizz = Rule(3,"Fizz")
Buzz = Rule(5,"Buzz")
Rules = [Fizz,Buzz]
#example 1
for x in range(0, 2):
    Rules[x].shout()
#example 2
for item in Rules:
    print (item.shout())

The output of example 1 is: Fizz Buzz as expected. But the output of example 2 is: Fizz None Buzz None

Why do they perform differently? Thank you in advance :)

Mikkel bruun
  • 129
  • 1
  • 10

1 Answers1

2

item.shout() returns a value of None

Therefore, when you print it, you'll get the print statement from the function as well as the return value which by default, is None

Josh Hamet
  • 957
  • 8
  • 10