[Solved] TypeError: method() takes 0 positional arguments but 1 was given


TypeError: method() takes 0 positional arguments but 1 was given

This error occurs when a method in Python is defined without the necessary self argument, but is called as if it expects one. The error message indicates that the method is expecting no arguments, but it implicitly receives self (the object itself) as the first argument.


Recreate Python TypeError

Below is an example that will trigger the TypeError: method() takes 0 positional arguments but 1 was given error.

Python Program

class Laptop:
	def details():
		print('Hello! I am a laptop.')

laptop1 = Laptop()
laptop1.details()

Explanation:

  1. In the class Laptop, the method details() is defined without any parameters.
  2. When we call laptop1.details(), Python implicitly passes laptop1 as the first argument to the method (this is self in instance methods).
  3. Since the method details() does not have a self parameter to accept the object reference, the method cannot handle the implicit argument, resulting in the TypeError.

Output

Traceback (most recent call last):
  File "example1.py", line 6, in <module>
    laptop1.details()
TypeError: details() takes 0 positional arguments but 1 was given

Solution for TypeError

To fix this error, you need to add self as the first parameter in the method definition. This allows the method to accept the implicit object reference passed by Python.

Python Program

class Laptop:
	def details(self):
		print('Hello! I am a laptop.')

laptop1 = Laptop()
laptop1.details()

Explanation:

  1. By adding self as the first parameter of the details() method, we now allow the method to accept the implicit reference to the object (laptop1) that calls it.
  2. When laptop1.details() is called, Python automatically passes laptop1 as self to the method, allowing the method to function as intended.

Output

Hello! I am a laptop.

Using Static Methods to Avoid Implicit self

If the method does not need access to instance-specific data, you can make it a static method. This way, the method will not require self as its first parameter.

Python Program

class Laptop:
	@staticmethod
	def details():
		print('Hello! I am a laptop.')

laptop1 = Laptop()
laptop1.details()

Explanation:

  1. The @staticmethod decorator indicates that the method does not depend on instance-specific data and does not require the self parameter.
  2. The method can now be called directly on the class or the instance, without passing any arguments. It does not expect an implicit self argument.
  3. Here, laptop1.details() works without any issues, as details() is now a static method.

Output

Hello! I am a laptop.

More Use Cases

1. Instance Method Without self

If you accidentally define an instance method without self, Python will raise the same TypeError because it expects the method to accept the object as the first argument.

Python Program

class Phone:
	def call():
		print('Calling...')

phone1 = Phone()
phone1.call()

Output

Traceback (most recent call last):
  File "example2.py", line 6, in <module>
    phone1.call()
TypeError: call() takes 0 positional arguments but 1 was given

Explanation:

  1. In the class Phone, the method call() is defined without the self parameter, which causes the same error when phone1.call() is called.
  2. The solution is to define the method with self as shown in the following program.

Python Program (Fixed)

class Phone:
	def call(self):
		print('Calling...')

phone1 = Phone()
phone1.call()

Output

Calling...

Summary

In this tutorial, we addressed the TypeError: method() takes 0 positional arguments but 1 was given error in Python. We showed how this error occurs when a method does not include self as its first parameter, and provided solutions for fixing the issue by either adding self or making the method static using the @staticmethod decorator. Additionally, we explored how to fix similar issues in instance methods.




Python Libraries