Bottom Page

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)
Quote
#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
Johnny's CookBook - Powered By: Python3.8 | Bootstrap4 | Apache2 | Django
Quote
#3
Methods need to be declared with self as the first parameter, e.g.

class Foo:
    def bar(self):
        pass

f = Foo()
f.bar()
nilamo likes this post
Quote
#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

Quote
#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
Quote
#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 ?
Quote
#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
newbieAuggie2019 likes this post
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  calling on a method from one class into another class which is not a child NABA 5 168 Apr-29-2020, 07:49 PM
Last Post: deanhystad
  Call a .xlsx file outside a class criscferr 2 136 Apr-24-2020, 04:23 PM
Last Post: criscferr
  SyntaxError: positional argument follows keyword argument syd_jat 3 1,010 Mar-03-2020, 08:34 AM
Last Post: buran
  Is it mandatory to call superclass init inside the class init? psolar 3 239 Feb-14-2020, 09:16 PM
Last Post: wavic
  threading for method outside class anna 0 165 Nov-17-2019, 07:05 AM
Last Post: anna
  How to call COM-method using comtypes jespersahner 0 216 Nov-15-2019, 12:54 PM
Last Post: jespersahner
  how can I changing a base class method voidptr 2 216 Nov-10-2019, 10:53 PM
Last Post: voidptr
  Polymorphism not working with a call to a abstract method colt 3 259 Nov-04-2019, 11:04 PM
Last Post: colt
  Call method from another method within a class anteboy65 3 441 Sep-11-2019, 08:40 PM
Last Post: Larz60+
  How to run a method on an argument in a formatted string Exsul 1 228 Aug-30-2019, 01:57 AM
Last Post: Exsul

Forum Jump:


Users browsing this thread: 1 Guest(s)