Last updated: Apr 8, 2024
Reading time·4 min
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()
.
Here is an example of how the error occurs.
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())
We called the get_name
method on the Employee
class rather than on an
instance of Employee
which caused the error.
To solve the error, instantiate the class and call the method on the instance.
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"
We instantiated the class and called the get_name
method on the instance.
__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.
__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.
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.
If your method doesn't make use of the self
argument, you can declare a
static method.
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"
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.
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"
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.
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.
Here is an example of how the error occurs.
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.
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: