user8506085
user8506085

Reputation:

Call function in class from non-class function

I basically want to call a function located inside a class from a function which is outside of any class, like the following example code:

class something(Gridlayout):
    def func1(self, number1, number2)
        Solution = number1 + number2

def call_func1()
       Something.func1(3, 5)

Of course this is only an example this isn't what I want to do, but the point is i tried the code above and it gives the following error:

TypeError: unbound method func1() must be called with something instance as first argument (got int instance instead)

What am I doing wrong??

Upvotes: 2

Views: 2231

Answers (3)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48047

Since func1 is a instance level function, you need to firstly create an object of the class something and then use it like:

my_object = something()
my_object.func1(3, 5)

But, as I see that you are not using self anywhere within your function, you may probably want to define it as a static method using @staticmethod decorator as:

@staticmethod
def func1(number1, number2):
    print('Numbers: {}, {}'.format(number1, number2)

and then call it the similar way as:

my_object = something()
my_object.func1(3, 5)

OR, if you want to access it without creating the object of the class, then you may define it as an class function using @classmethod decorator as:

@classmethod
def func1(cls, number1, number2):
    #      ^ Reference of class will be passed here, instead of object of class
    print('Numbers: {}, {}'.format(number1, number2)

and then you may call it directly using the class as:

something.func1(3,5)    

Upvotes: 2

Abhijit Pritam Dutta
Abhijit Pritam Dutta

Reputation: 5581

I think you did not import the class to your program. If you are calling call_func1() from the same python file that it O.K. but if you declared the class in a python file call something.py and you have a test.py python file from where you are calling call_func1(), in that file (test.py) you have to add the below line at the top of the file.

in something.py

class something(Gridlayout):
    def func1(self, number1, number2)
        Solution = number1 + number2
        return Solution

in test.py

from something import Something

def call_func1()
   Something something
   val=something.func1(3, 5)
   print(val)

if you declare func1 as static you can use Something.func1 no need to instantiate it.

Also in your program I notice one syntax error. You are declaring your class as something(small s) and you are referring it as Something (S capital). Hope this will help you.

Upvotes: 1

jmd_dk
jmd_dk

Reputation: 13090

What you are looking for is a method which has no reference to self. That is, simply a normal function which happens to live on the class. This is known as a static method, and can be constructed via the staticmethod decorator:

class Something(Gridlayout):
    @staticmethod
    def func1(number1, number2):
        Solution = number1 + number2
        return Solution

def call_func1():
   Solution = Something.func1(3, 5)
   print(Solution)

call_func1()

Upvotes: 1

Related Questions