TypeError: Missing 1 required positional argument: 'self'
Question:
I have some code like:
class Pump:
def __init__(self):
print("init")
def getPumps(self):
pass
p = Pump.getPumps()
print(p)
But I get an error like:
Traceback (most recent call last):
File "C:UsersDomDesktoptesttest.py", line 7, in <module>
p = Pump.getPumps()
TypeError: getPumps() missing 1 required positional argument: 'self'
Why doesn’t __init__
seem to be called, and what does this exception mean? My understanding is that self
is passed to the constructor and methods automatically. What am I doing wrong here?
Answers:
To use the class, first create an instance, like so:
p = Pump()
p.getPumps()
A full example:
>>> class TestClass:
... def __init__(self):
... print("init")
... def testFunc(self):
... print("Test Func")
...
>>> testInstance = TestClass()
init
>>> testInstance.testFunc()
Test Func
You need to initialize it first:
p = Pump().getPumps()
You can also get this error by prematurely taking PyCharm’s advice to annotate a method @staticmethod. Remove the annotation.
The self
keyword in Python is analogous to this
keyword in C++ / Java / C#.
In Python 2 it is done implicitly by the compiler (yes Python does compilation internally).
It’s just that in Python 3 you need to mention it explicitly in the constructor and member functions. example:
class Pump():
# member variable
# account_holder
# balance_amount
# constructor
def __init__(self,ah,bal):
self.account_holder = ah
self.balance_amount = bal
def getPumps(self):
print("The details of your account are:"+self.account_number + self.balance_amount)
# object = class(*passing values to constructor*)
p = Pump("Tahir",12000)
p.getPumps()
Works and is simpler than every other solution I see here :
Pump().getPumps()
This is great if you don’t need to reuse a class instance. Tested on Python 3.7.3.
Adding a @classmethod
decorator to the method allows for calling it like Pump.getPumps()
.
A class method receives the class as the implicit first argument, just like an instance method receives the instance.
class Pump:
def __init__(self):
print("init")
@classmethod
def getPumps(cls):
pass
I got the same error below:
TypeError: test() missing 1 required positional argument: ‘self’
When an instance method had self
, then I called it directly by class name as shown below:
class Person:
def test(self): # <- With "self"
print("Test")
Person.test() # Here
And, when a static method had self
, then I called it by object or directly by class name as shown below:
class Person:
@staticmethod
def test(self): # <- With "self"
print("Test")
obj = Person()
obj.test() # Here
# Or
Person.test() # Here
So, I called the instance method with object as shown below:
class Person:
def test(self): # <- With "self"
print("Test")
obj = Person()
obj.test() # Here
And, I removed self
from the static method as shown below:
class Person:
@staticmethod
def test(): # <- "self" removed
print("Test")
obj = Person()
obj.test() # Here
# Or
Person.test() # Here
Then, the error was solved:
Test
In detail, I explain about instance method in my answer for What is an "instance method" in Python? and also explain about @staticmethod and @classmethod in my answer for @classmethod vs @staticmethod in Python.
If skipping parentheses for the object declaration (typo), then exactly this error occurs.
# WRONG! will result in TypeError: getPumps() missing 1 required positional argument: 'self'
p = Pump
p.getPumps()
Do not forget the parentheses for the Pump object
# CORRECT!
p = Pump()
p.getPumps()
I have some code like:
class Pump:
def __init__(self):
print("init")
def getPumps(self):
pass
p = Pump.getPumps()
print(p)
But I get an error like:
Traceback (most recent call last):
File "C:UsersDomDesktoptesttest.py", line 7, in <module>
p = Pump.getPumps()
TypeError: getPumps() missing 1 required positional argument: 'self'
Why doesn’t __init__
seem to be called, and what does this exception mean? My understanding is that self
is passed to the constructor and methods automatically. What am I doing wrong here?
To use the class, first create an instance, like so:
p = Pump()
p.getPumps()
A full example:
>>> class TestClass:
... def __init__(self):
... print("init")
... def testFunc(self):
... print("Test Func")
...
>>> testInstance = TestClass()
init
>>> testInstance.testFunc()
Test Func
You need to initialize it first:
p = Pump().getPumps()
You can also get this error by prematurely taking PyCharm’s advice to annotate a method @staticmethod. Remove the annotation.
The self
keyword in Python is analogous to this
keyword in C++ / Java / C#.
In Python 2 it is done implicitly by the compiler (yes Python does compilation internally).
It’s just that in Python 3 you need to mention it explicitly in the constructor and member functions. example:
class Pump():
# member variable
# account_holder
# balance_amount
# constructor
def __init__(self,ah,bal):
self.account_holder = ah
self.balance_amount = bal
def getPumps(self):
print("The details of your account are:"+self.account_number + self.balance_amount)
# object = class(*passing values to constructor*)
p = Pump("Tahir",12000)
p.getPumps()
Works and is simpler than every other solution I see here :
Pump().getPumps()
This is great if you don’t need to reuse a class instance. Tested on Python 3.7.3.
Adding a @classmethod
decorator to the method allows for calling it like Pump.getPumps()
.
A class method receives the class as the implicit first argument, just like an instance method receives the instance.
class Pump:
def __init__(self):
print("init")
@classmethod
def getPumps(cls):
pass
I got the same error below:
TypeError: test() missing 1 required positional argument: ‘self’
When an instance method had self
, then I called it directly by class name as shown below:
class Person:
def test(self): # <- With "self"
print("Test")
Person.test() # Here
And, when a static method had self
, then I called it by object or directly by class name as shown below:
class Person:
@staticmethod
def test(self): # <- With "self"
print("Test")
obj = Person()
obj.test() # Here
# Or
Person.test() # Here
So, I called the instance method with object as shown below:
class Person:
def test(self): # <- With "self"
print("Test")
obj = Person()
obj.test() # Here
And, I removed self
from the static method as shown below:
class Person:
@staticmethod
def test(): # <- "self" removed
print("Test")
obj = Person()
obj.test() # Here
# Or
Person.test() # Here
Then, the error was solved:
Test
In detail, I explain about instance method in my answer for What is an "instance method" in Python? and also explain about @staticmethod and @classmethod in my answer for @classmethod vs @staticmethod in Python.
If skipping parentheses for the object declaration (typo), then exactly this error occurs.
# WRONG! will result in TypeError: getPumps() missing 1 required positional argument: 'self'
p = Pump
p.getPumps()
Do not forget the parentheses for the Pump object
# CORRECT!
p = Pump()
p.getPumps()