Why do I get TypeError Missing 1 required positional argument 'self'?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Python error TypeError: missing 1 required positional argument: 'self' occurs when you call an instance method on the class itself instead of on an instance. For example, MyClass.method() instead of MyClass().method(). Python instance methods expect the first argument to be the object instance (conventionally named self), which is automatically passed when calling the method on an instance. Calling it on the class directly skips this automatic binding, causing the error.
The Error
When you call dog.bark(), Python translates it to Dog.bark(dog) behind the scenes. Calling Dog.bark() passes no arguments, so self is missing.
Common Cause 1: Forgetting to Create an Instance
Common Cause 2: Missing Parentheses in Constructor
Common Cause 3: Passing Class Instead of Instance to a Function
Common Cause 4: Method as Callback Without Binding
When to Use @staticmethod or @classmethod
If a method does not need access to the instance, use @staticmethod:
| Decorator | First Parameter | Callable On |
| (none) | self (instance) | Instance only |
@classmethod | cls (class) | Class or instance |
@staticmethod | (none) | Class or instance |
Debugging the Error
The Error with init
Common Pitfalls
- Variable shadowing the class: If you have
User = User("admin"), subsequentUser("new")calls fail becauseUseris now an instance, not the class. Use distinct names for classes and instances. - Forgetting parentheses in decorator patterns:
@app.routevs@app.route()— missing parentheses can cause the decorator to pass the function asselfto the wrong method. - Calling instance methods in list comprehensions:
[MyClass.method(x) for x in data]fails. Use[instance.method(x) for x in data]or create the instance first. - Confusing
@staticmethodneed: If your method does not useself, add@staticmethod. Without it, callingMyClass.method()always raises the missingselferror even though the method body does not useself. - Inheritance with
super(): Callingsuper().method()is correct. CallingParentClass.method()without passingselfexplicitly raises this error. Always usesuper()for parent method calls.
Summary
- The error occurs when calling an instance method on the class instead of on an instance
- Always create an instance with
MyClass()before calling instance methods - Use
@staticmethodfor methods that do not needself, and@classmethodfor methods that need the class - Check
type(obj)to verify you have an instance, not the class itself - Python translates
obj.method()toClass.method(obj)— callingClass.method()directly omits the instance

