TypeError: takes 0 positional arguments but 1 was given
Question:
Help me what am I making wrong here since am getting the below error,
TypeError: fizz_buzz() takes 0 positional arguments but 1 was given
class FizzBuzz:
def __init__(self, number_value):
self.number_value = number_value
def fizz_buzz():
if number_value % 3 == 0 and number_value % 5 == 0:
print("FizzBuzz")
elif number_value % 3 == 0:
print("Fizz")
elif number_value % 5 == 0:
print("Buzz")
else:
return f"{number_value} can't be multiplied by either 3 or 5"
number_value = int(input("Enter number: "))
fizzbuzz_object = FizzBuzz(number_value)
fizzbuzz_object.fizz_buzz()
Answers:
added the below code which fixed the issue:
`def fizz_buzz(self):`
Make sure that when you are creating class methods you always have 1 argument called “self”:
def fizz_buzz(self):
if number_value % 3 == 0 and number_value % 5 == 0:
print("FizzBuzz")
elif number_value % 3 == 0:
print("Fizz")
elif number_value % 5 == 0:
print("Buzz")
else:
return f"{number_value} can't be multiplied by either 3 or 5"
You need to reference the current (self) instance of the class. Try:
def fizz_buzz(self):
Instead of:
def fizz_buzz():
this means you should have all functions inside a class with atleast one argument,
def fizz_buzz(self):
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
An instance method needs self
in the 1st argument as shown below:
class Person:
# Here
def test(self):
print("Test1")
obj = Person()
obj.test1()
Output:
Test1
In detail, I explain about instance method in my answer for What is an "instance method" in Python? and also explain about @classmethod and @staticmethod in my answer for @classmethod vs @staticmethod in Python:
Help me what am I making wrong here since am getting the below error,
TypeError: fizz_buzz() takes 0 positional arguments but 1 was given
class FizzBuzz:
def __init__(self, number_value):
self.number_value = number_value
def fizz_buzz():
if number_value % 3 == 0 and number_value % 5 == 0:
print("FizzBuzz")
elif number_value % 3 == 0:
print("Fizz")
elif number_value % 5 == 0:
print("Buzz")
else:
return f"{number_value} can't be multiplied by either 3 or 5"
number_value = int(input("Enter number: "))
fizzbuzz_object = FizzBuzz(number_value)
fizzbuzz_object.fizz_buzz()
added the below code which fixed the issue:
`def fizz_buzz(self):`
Make sure that when you are creating class methods you always have 1 argument called “self”:
def fizz_buzz(self):
if number_value % 3 == 0 and number_value % 5 == 0:
print("FizzBuzz")
elif number_value % 3 == 0:
print("Fizz")
elif number_value % 5 == 0:
print("Buzz")
else:
return f"{number_value} can't be multiplied by either 3 or 5"
You need to reference the current (self) instance of the class. Try:
def fizz_buzz(self):
Instead of:
def fizz_buzz():
this means you should have all functions inside a class with atleast one argument,
def fizz_buzz(self):
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
An instance method needs self
in the 1st argument as shown below:
class Person:
# Here
def test(self):
print("Test1")
obj = Person()
obj.test1()
Output:
Test1
In detail, I explain about instance method in my answer for What is an "instance method" in Python? and also explain about @classmethod and @staticmethod in my answer for @classmethod vs @staticmethod in Python: