Explain Codes LogoExplain Codes Logo

"typeerror: method() takes 1 positional argument but 2 were given" but I only passed one

python
self
python-methods
error-handling
Anton ShumikhinbyAnton Shumikhin·Mar 8, 2025
TLDR

The emergence of this TypeError indicates a discrepancy between the required and provided arguments, typically due to missing self in method definitions. Instance methods need self to exemplify the object instance.

Quick Fix: Incorporate self into your method signature:

class MyClass: def my_method(self, arg): # don't forget your 'self'! print(arg) # Prints 'Hello'. No errors. Life is good. instance = MyClass() instance.my_method("Hello") # Just remember, self comes first!

This style ensures that the method accepts the instance reference (self) together with any other parameters.

Deep dive into 'self'

Python methods carry an extra passenger - the object they're tied to. When you define a class method, you might not notice its silent companion. But when you call this method on an object, the object itself is implicitly fed in as the first argument.

Ergo, MyClass.my_method(instance, "Hello") is the real party happening behind the curtains. The instance (self) is our VIP guest here, slipping in silently. So, when you forget self, Python starts counting party crashers.

Jumping through hoops: Avoiding common traps

Python methods aren't complicated, but there are some things to watch out for:

  • Always include self in your instance method signatures. It's not just a token, it holds the key to the method's home.
  • If a method is no Drama Queen (read: doesn't interact with the object's state), swank it up with @staticmethod. You get to drop the self.
  • @classmethod is the magic charm you need when the method seeks audience with the class itself, not its subjects. Join the royal court with cls, not self.
  • A TypeError rampage may be a cry for help from a misconfigured __init__ method. Double-check your syntax and ensure self is the first argument.
  • Unpacking arguments beautifully from a dictionary using **mydict can save your day if your method is expecting keyword arguments.

On a side note, syntax errors and typos can lead to these pesky little TypeError bugs too. Code reviews aren't just for tracing logical errors.

Case studies for effective troubleshooting

Let's put our detective hat on and diagnose potential errors:

  • Static methods: Use the @staticmethod decorator when neither self nor cls is needed by the method, making it pure & drama-free!
  • Classy methods: For @classmethods, make sure cls holds the sceptre, not self. It rules the class, after all!
  • The __init__ pitfall: A properly configured __init__ can save a lot of TypeError heartache. self comes first, self comes first, self comes first. It's a mantra!
  • Unlocking dictionaries with style: When a method yearns for keyword arguments, deconstructing a dictionary with **mydict is a hassle-free breakthrough.

These methodological detective techniques can help you avert menacing TypeError threats in Python.