TypeError: missing 1 required positional argument: 'self'

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# Table of Contents

  1. TypeError: missing 1 required positional argument: 'self'
  2. load() missing 1 required positional argument: 'Loader'

# TypeError: missing 1 required positional argument: 'self'

The Python "TypeError: missing 1 required positional argument: 'self'" occurs when we call a method on the class instead of on an instance of the class.

To solve the error, instantiate the class first and call the method on the instance, e.g. emp1.get_name().

typeerror missing 1 required positional argument self

Here is an example of how the error occurs.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): return self.name # ⛔️ TypeError: Employee.get_name() missing 1 required positional argument: 'self' print(Employee.get_name())

missing 1 required positional argument self

We called the get_name method on the Employee class rather than on an instance of Employee which caused the error.

# Instantiate the class and call the method on an instance

To solve the error, instantiate the class and call the method on the instance.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): return self.name # ✅ Instantiate the class first emp1 = Employee('Bobby Hadz', 100) # ✅ Call the method on the class instance print(emp1.get_name()) # 👉️ "Bobby Hadz"

instantiate class and call method on an instance

We instantiated the class and called the get_name method on the instance.

When we instantiate a class, we should provide all the arguments that we have specified in the class's __init__() method (other than self which is passed automatically).

When a class defines the __init__() method, the method is invoked when an instance is created.

If your class doesn't define an __init__() method, you don't have to pass any arguments when instantiating it.

If you pass arguments when instantiating a class, the arguments are passed on to the __init__() method.

Note that the first argument the __init__() method takes is self.

You could name this argument anything because the name self has no special meaning in Python.

self represents an instance of the class, so when we assign a variable as self.my_var = 'some value', we are declaring an instance variable - a variable unique to each instance.

The "TypeError: missing 1 required positional argument: 'self'" occurs because we specified the self argument in the get_name method.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary # 👇️ specified self arg def get_name(self): return self.name emp1 = Employee('Bobby Hadz', 100) print(emp1.get_name()) # 👉️ "Bobby Hadz"

If we call the get_name method on an instance of the class, Python automatically passes self to the class method.

# Using a static class method instead of an instance method

If your method doesn't make use of the self argument, you can declare a static method.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary @staticmethod def get_name(): return 'Bobby Hadz' print(Employee.get_name()) # 👉️ "Bobby Hadz"

using static class method instead of instance method

A static method does not receive an implicit first argument and can be called on the class or on an instance of the class.

You can also transform the method into a class method using the @classmethod decorator.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary @classmethod def get_name(cls): print(cls) return 'Alice' print(Employee.get_name()) # 👉️ "Bobby Hadz"

transform method into class method

A class method gets passed the class as an implicit first argument, just like an instance method gets passed the instance.

You can call a class method on the class (Employee.get_name()) or on an instance of the class (Employee('Alice', 100).get_name()).

If you call a class method on an instance of the class, the instance is ignored except for its class.

# load() missing 1 required positional argument: 'Loader'

The Python "TypeError: load() missing 1 required positional argument: 'Loader'" occurs when we use the yaml.load() method without specifying the Loader keyword argument.

To solve the error, use the yaml.full_load() method instead or explicitly set the Loader keyword arg.

typeerror load missing 1 required positional argument loader

Here is an example of how the error occurs.

main.py
import yaml document = """ a: 1 b: c: 3 d: 4 """ # ⛔️ TypeError: load() missing 1 required positional argument: 'Loader' print(yaml.dump(yaml.load(document)))

The yaml.load method now requires us to explicitly specify the Loader keyword arguments because of some security implications around the default behavior of the method.

The easiest way to solve the error is to use one of the yaml.full_load() or yaml.safe_load() methods.

main.py
import yaml document = """ a: 1 b: c: 3 d: 4 """ print(yaml.dump(yaml.safe_load(document))) print(yaml.dump(yaml.full_load(document)))

The examples above achieve the same result as explicitly passing the Loader keyword argument in a call to the yaml.load() method.

main.py
import yaml document = """ a: 1 b: c: 3 d: 4 """ print(yaml.dump(yaml.load(document, Loader=yaml.SafeLoader))) print(yaml.dump(yaml.load(document, Loader=yaml.FullLoader)))

The SafeLoader which is used by the yaml.safe_load() method loads a subset of the YAML language. This is recommended for loading untrusted input.

The yaml.safe_load method should be used unless you need to serialize or deserialize arbitrary objects because the method cannot execute arbitrary code from the YAML file.

The FullLoader which is used by the yaml.full_load method loads the full YAML language. Using this with untrusted input is not recommended.

You can read more about why the default behavior of the yaml.load method was deprecated in this Github wiki.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.