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.
Gaming Collection
Homepage
my-python
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
  anonymous method in a class Skaperen 8 1,041 May-23-2021, 11:17 PM
Last Post: Skaperen
  How to apply a class method to an entire dataframe column tirtha9 1 781 Jan-03-2021, 04:44 AM
Last Post: klllmmm
  NameError when calling a class method mfreudenberg 2 706 Sep-25-2020, 07:40 AM
Last Post: mfreudenberg
  Using one method's result for another method in the same Class? btownboy 3 934 Sep-13-2020, 06:37 AM
Last Post: buran
  Return boolean from recursive class method medatib531 6 1,014 Jul-13-2020, 04:27 AM
Last Post: medatib531
  referencing another method in a class Skaperen 6 977 Jul-02-2020, 04:30 AM
Last Post: Skaperen
  how to get around recursive method call Skaperen 10 1,683 Jul-01-2020, 10:09 PM
Last Post: Skaperen
  How to emit a signal from another Class Method? SKDN 1 1,303 Jun-30-2020, 05:44 PM
Last Post: Yoriz
  calling on a method from one class into another class which is not a child NABA 5 992 Apr-29-2020, 07:49 PM
Last Post: deanhystad
  Call a .xlsx file outside a class criscferr 2 786 Apr-24-2020, 04:23 PM
Last Post: criscferr

Forum Jump:

User Panel Messages

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