HOWTO · Python
How to Fix Python TypeError: Missing 1 Required Positional Argument
This tutorial discusses the TypeError: missing 1 required positional argument: self in Python.
On this page
The TypeError: missing 1 required positional argument: 'self' usually means an instance method was called without an instance. In a method such as def fun(self), self is the conventional first parameter; Python supplies it when the method is called through an object. It is a parameter, not an attribute. Create the object first, then call the method on that object.
What “missing 1 required positional argument: ‘self’” means
A function declared in a class without a decorator is an instance method. When you access it through an instance, Python binds that instance to the method and passes it as self. When you access the same method through the class, Python does not have an instance to bind, so the first required parameter remains unfilled.
The wording identifies the missing parameter, not necessarily a missing value from the method’s main task. If a method is declared def greet(self, name), calling it without a bound instance can leave self missing even if you supplied name. This is a TypeError caused by the call’s arguments.
How calling an instance method on the class causes the error
Calling Delft.fun() asks Python to call the function through the class. If fun expects self, provide an instance explicitly or, more clearly, call the method on an instance. A related mistake is assigning the class itself to a variable: m = Delft does not construct an object. Then m.fun() still accesses the method through the class and leaves self unbound.
If the constructor requires data, that data belongs in the construction call, for example Delft("Stack"). Omitting the constructor parentheses means no instance was created; it does not pass a value for self. The class remains callable as a class, and the method is not inherently unsupported through class access—the call simply needs to match the method’s parameters.
Fix the call by creating an instance
Construct an instance with any required constructor arguments, store it, and call the method on that instance. This self-contained example shows the usual pattern:
class Delft:
def __init__(self, name):
self.name = name
def fun(self):
return self.name
m = Delft("Stack")
print(m.fun())
This example runs successfully and prints Stack. The call binds m to self, and the method returns the name stored during construction.
When a static method is appropriate
A static method does not receive an instance automatically. Use @staticmethod when an operation belongs conceptually with the class but does not read or change instance state. For example, a class could expose a formatting helper that only uses its explicit argument. Call that helper on the class or an instance as appropriate to your API.
Changing an instance method into a static method is not a general fix for this TypeError. If the operation uses self.name or other per-object data, keep it as an instance method and create an object. Otherwise, removing the unused self parameter and adding @staticmethod can make the intended calling convention explicit.
Check the method definition and call site
Read the method signature and match each required parameter with the call. If the first parameter is self, confirm that the method is called on an object created with parentheses, rather than on the class or a variable that merely refers to the class. Also check that constructor arguments are passed when the object is created and that ordinary method arguments are passed when the method is called.
For example, m = Delft stores the class, while m = Delft("Stack") creates an instance when the constructor takes a name. Then m.fun() supplies the bound instance automatically. If the method genuinely needs no instance data, consider @staticmethod; otherwise, preserve the instance method and correct the call site.