Here is the detailed answer:
fizzbuzz_object.fizz_buzz() # you did not pass the instance. "self" is just a symbol that represents the object
fizz_buzz()
is a method. Methods are object type in Python. methods are callable like functions but they are bound some object and that object is injected to the method as its first parameter. That is why class methods are called "instance methods".if you run this:
print(fizzbuzz_object.fizz_buzz)
you will get this:
<bound method FizzBuzz.fizz_buzz of <__main__.FizzBuzz object at 0x7ffa306574f0>>
Python sees .fizz_buzz
the dot and it knows that this method is bound to the object. this is when fizz_buzz
turns to be a method. So far it was considered to be a function. This is actually difference between methods and functions. methods are bound.
type(FizzBuzz.fizz_buzz) is type(fizzbuzz_object.fizz_buzz)
this will return false
. First type is "function", second type is "method"
If you called `FizzBuzz.fizz_buzz` like this you will not get same error.
Behind the scene this is what python calling
FizzBuzz.fizz_buzz(fizzbuzz_object)
With injecting the object into the method, methods can access to the object's namespace. Also this passes 2 attributes to methods
print(fizzbuzz_object.fizz_buzz.__self__)
print(fizzbuzz_object.fizz_buzz.__func__)
<__main__.FizzBuzz object at 0x7ffa306574f0> // object
<function FizzBuzz.fizz_buzz at 0x7ffa306620d0> // method