2

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??

1
  • 1
    func1 is an instance method. So you first need to construct a Something instance, and then call func1 on that instance. Commented Oct 22, 2017 at 20:53

3 Answers 3

2

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)    
Sign up to request clarification or add additional context in comments.

3 Comments

Nothing happens when I try to do that..?
@HussamAl-hassan Add a print statement inside the function and you'll see the function getting called
Now I have another problem.. I'm trying to call a function which changes the text of a button in kivy from the function your telling me to make static and it's not working (I'm trying to do: self.button1.text = "new text")
1

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()

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.