-3
Module1.py

Class A ():
   def func1(self,...):
      .....


   def func2(self,...):
      .....

Module2.py

   def callfunc():
      How to call func2() from Module1.py

In Module2.py, I tried using

from Module1 import A

       def callfunc():
          A.func2()

but it throws an error stating TypeError: unbound method **func2()** must be called with **A** instance as first argument (got list instance instead)

Could someone tell me how to call func2() in Module2.py ?

prime
  • 1
  • 2

1 Answers1

1

Your import is fine, the problem is that you need an instance of A to call the function from

def callfunc():
    a = A()
    a.func2()

This is because func2 is a bound method, in other words it needs an instance of the class to operate on, hence the self argument.

def func2(self,...):

For you to be able to call it off the class itself, it would be a static function and wouldn't require an instance of the class

def func2():
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • In the end you said instance is not required, then what is the statement i should use ? – prime Sep 09 '15 at 17:24
  • Read more carefully, you **do** need an instance. The last sentence is showing what an **unbound** static function would look like, which is not what your case is. – Cory Kramer Sep 09 '15 at 17:26