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


Messages In This Thread
How to Call a method of class having no argument - by dataplumber - Oct-30-2019, 06:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 266 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 756 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  method call help sollarriiii 6 1,162 Feb-21-2023, 03:19 AM
Last Post: noisefloor
  Using one child class method in another child class garynewport 5 1,607 Jan-11-2023, 06:07 PM
Last Post: garynewport
  i want to use type= as a function/method keyword argument Skaperen 9 1,881 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  How to call a class in other class? 3lnyn0 3 938 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,322 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,483 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,802 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,716 Oct-30-2021, 11:20 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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