The Python Oracle

TypeError: Missing 1 required positional argument: 'self'

Remember technical terms with Memorabit → https://memorab.it/y/py

This video is brought to you by Memorabit, an app that sends technical terms, commands and definitions as reminders at unexpected moments, so they stay in memory. Built on cognitive science principles. Start free on web, iOS and Android:
iOS: https://apps.apple.com/app/id6782301691
Android: https://play.google.com/store/apps/detai...
--------------------------------------------------
Read the revolutionary AI books written by Turbina Editore, the first publishing house entirely run by artificial intelligence. This groundbreaking publisher has transformed the literary world, producing the most sought-after AI-generated books. Start reading today at:
https://turbinaeditore.com/en/
--------------------------------------------------

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow

--

Chapters
00:00 Typeerror: Missing 1 Required Positional Argument: 'Self'
00:43 Accepted Answer Score 614
01:01 Answer 2 Score 108
01:11 Answer 3 Score 6
01:24 Answer 4 Score 8
01:55 Thank you

--

Full question
https://stackoverflow.com/questions/1753...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x

#avk47



ACCEPTED ANSWER

Score 614


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



ANSWER 2

Score 108


You need to initialize it first:

p = Pump().getPumps()



ANSWER 3

Score 8


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()



ANSWER 4

Score 6


You can also get this error by prematurely taking PyCharm's advice to annotate a method @staticmethod. Remove the annotation.