Python Forum
How to Call a method of class having no argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Call a method of class having no argument
#1
I am trying to call the method of the class having no arguments but it is failing.

class ab:
        def a():
                print("in a")
def b():
        print("in b")

if __name__ == '__main__':
        b()
        ab.a()
Error:
in b Traceback (most recent call last): File "p.py", line 11, in <module> ab.a() TypeError: unbound method a() must be called with ab instance as first argument (got nothing instead)
I have also tried to modify the code as below but not successful

class ab:
        def a():
                print("in a")
def b():
        print("in b")

if __name__ == '__main__':
        b()
        c=ab()
        c.a()
Error:
in b Traceback (most recent call last): File "p.py", line 12, in <module> c.a() TypeError: a() takes no arguments (1 given)
Reply
#2
# Could be wrong but
if __name__ == "__main__"
# is looking for the def main
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Methods need to be declared with self as the first parameter, e.g.

class Foo:
    def bar(self):
        pass

f = Foo()
f.bar()
Reply
#4
Alternatively you can define it as a static method:

>>> class Foo(object):
...    @staticmethod
...    def bar():
...       print('bar')
...
>>> f = Foo()
>>> f.bar()
bar
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Two issues. As above, need self in the function definition. Next, you need an ab object in order to call the function. The class is just a template. So, in the example below, we create the cab object of type ab, then call the function:
class ab:
        def a(self):
                print("in a")
def b():
        print("in b")
 
if __name__ == '__main__':
        b()
        cab = ab()
        cab.a()
Output:
in b in a
Reply
#6
(Oct-30-2019, 06:24 PM)ndc85430 Wrote: Methods need to be declared with self as the first parameter, e.g.
 class Foo: def bar(self): pass f = Foo() f.bar() 


Yes, with self as argument it is working. That being said, Is it necessary to have all the class methods should have at least 1 argument (self) in our case. Can't we have no arguments ?
Reply
#7
(Oct-31-2019, 12:03 PM)dataplumber Wrote: Is it necessary to have all the class methods should have at least 1 argument (self) in our case
all instance and class methods should have one argument. For instance methods, by convention, we call that argument self. For class methods we use, again by convention, cls.
And there are staticmethods that answer to your next question:
(Oct-31-2019, 12:03 PM)dataplumber Wrote: Can't we have no arguments ?

As ichabood explained
you need to declare it staticmethod. However in this case the question is does it really belong to a class or can simply be a function, outside class. i.e. if it doesn't need to access instance or the class

Google the difference between instance, class, static methods and simple function
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(Oct-31-2019, 12:03 PM)dataplumber Wrote:
(Oct-30-2019, 06:24 PM)ndc85430 Wrote: Methods need to be declared with self as the first parameter, e.g.
 class Foo: def bar(self): pass f = Foo() f.bar() 
Yes, with self as argument it is working. That being said, Is it necessary to have all the class methods should have at least 1 argument (self) in our case. Can't we have no arguments ?

Thank you. That Answers my question.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() and order of running method in class inheritance akbarza 7 594 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  method call help sollarriiii 6 1,078 Feb-21-2023, 03:19 AM
Last Post: noisefloor
  Using one child class method in another child class garynewport 5 1,484 Jan-11-2023, 06:07 PM
Last Post: garynewport
  i want to use type= as a function/method keyword argument Skaperen 9 1,772 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  How to call a class in other class? 3lnyn0 3 890 Oct-24-2022, 09:18 AM
Last Post: GenTorossi
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,267 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,380 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,680 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,657 Oct-30-2021, 11:20 PM
Last Post: Yoriz
Question How to pass a method as argument in an another method? anilanvesh 6 2,664 Sep-30-2021, 10:18 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020