Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign external function to class variable in Python

I am trying to assign a function defined elsewhere to a class variable so I can later call it in one of the methods of the instance, like this:

from module import my_func

class Bar(object):
    func = my_func
    def run(self):
        self.func()  # Runs my function

The problem is that this fails because when doing self.func(), then the instance is passed as the first parameter.

I've come up with a hack but seems ugly to me, anybody has an alternative?

In [1]: class Foo(object):
   ...:     func = lambda *args: args
   ...:     def __init__(self):
   ...:         print(self.func())
   ...:

In [2]: class Foo2(object):
   ...:     funcs = [lambda *args: args]
   ...:     def __init__(self):
   ...:         print(self.funcs[0]())
   ...:

In [3]: f = Foo()
(<__main__.Foo object at 0x00000000044BFB70>,)

In [4]: f2 = Foo2()
()

Edit: The behavior is different with builtin functions!

In [13]: from math import pow

In [14]: def pow_(a, b):
   ....:     return pow(a, b)
   ....:

In [15]: class Foo3(object):
   ....:     func = pow_
   ....:     def __init__(self):
   ....:         print(self.func(2, 3))
   ....:

In [16]: f3 = Foo3()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-c27c8778655e> in <module>()
----> 1 f3 = Foo3()

<ipython-input-15-efeb6adb211c> in __init__(self)
      2     func = pow_
      3     def __init__(self):
----> 4         print(self.func(2, 3))
      5

TypeError: pow_() takes exactly 2 arguments (3 given)

In [17]: class Foo4(object):
   ....:     func = pow
   ....:     def __init__(self):
   ....:         print(self.func(2, 3))
   ....:

In [18]: f4 = Foo4()
8.0
like image 202
astrojuanlu Avatar asked Jun 29 '16 10:06

astrojuanlu


People also ask

How do you call a function outside the class in Python?

var_name. If you want to use that variable even outside the class, you must declared that variable as a global. Then the variable can be accessed using its name inside and outside the class and not using the instance of the class.

Can I assign a function to a variable Python?

In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Simply assign a function to the desired variable but without () i.e. just with the name of the function.

How do you use the variable outside function in Python?

Using the global statement If you want to assign a value to a name defined outside the function, then you have to tell Python that the name is not local, but it is global. We do this using the global statement. It is impossible to assign a value to a variable defined outside a function without the global statement.

How do you call a function inside a class in Python?

To call a function within class with Python, we call the function with self before it. We call the distToPoint instance method within the Coordinates class by calling self. distToPoint . self is variable storing the current Coordinates class instance.


1 Answers

Python functions are descriptor objects, and when attributes on a class accessing them an instance causes them to be bound as methods.

If you want to prevent this, use the staticmethod function to wrap the function in a different descriptor that doesn't bind to the instance:

class Bar(object):
    func = staticmethod(my_func)
    def run(self):
        self.func()

Alternatively, access the unbound function via the __func__ attribute on the method:

def run(self):
    self.func.__func__()

or go directly to the class __dict__ attribute to bypass the descriptor protocol altogether:

def run(self):
    Bar.__dict__['func']()

As for math.pow, that's not a Python function, in that it is written in C code. Most built-in functions are written in C, and most are not descriptors.

like image 167
Martijn Pieters Avatar answered Oct 18 '22 09:10

Martijn Pieters