Is it possible to store a certain function as a variable?

If not, is it okay to make a class, and define a function. Store the class as the variable and invoke that function?

Recommended Answers

All 8 Replies

Not unless you are going to use "exec()" and people advise against that.

The second way is a good way.

class Test:
    def Testing(self):
        # Run function

x = Test()
x.Testing()

Simply store the reference to the function in the variable. Here is example:

def one():
    print("hello from function one")

def two():
    print("hello from function two")

def three():
    print("hello from function three")

# stores the ref of function in variable
f1 = one
f2 = two
f3 = three
func_list = [f1, f2, f3]

for f in func_list:
    # show the function reference
    print(f)
    # call the function
    f()

"""my display -->
<function one at 0x01DFE150>
hello from function one
<function two at 0x01DFE198>
hello from function two
<function three at 0x01DFE108>
hello from function three
"""

Simply store the reference to the function in the variable. Here is example:

def one():
    print("hello from function one")

def two():
    print("hello from function two")

def three():
    print("hello from function three")

# stores the ref of function in variable
f1 = one
f2 = two
f3 = three
func_list = [f1, f2, f3]

for f in func_list:
    # show the function reference
    print(f)
    # call the function
    f()

"""my display -->
<function one at 0x01DFE150>
hello from function one
<function two at 0x01DFE198>
hello from function two
<function three at 0x01DFE108>
hello from function three
"""

I never knew I could do that. I knew you could do it with a class instance but...thanks

Awesome. That solves a tonne of problems. Thanks. =)

I need help on my Python project. I am creating a guess the number game and I need to include a menu, collect statistics, and plot statistics results. I have already created the game, but I don't know how to include a menu, collect statistics, or plot the results. Please help!!

Hi,
I too have same functinality like you guys posted here...
it iis working fine when I pass arguments to defined functions.

going wrong if the function is specified with "self"

Ex:

class Anyclassname:
    def __init__ (self, a):
        self.a = a
    def one(self, dicti):
        print("hello from function one")
        print(self.a, dicti)

 ## Assume that instance is created  function is called refering eith instance

 for f in func_list:
     # show the function reference
     print(f)
     # call the function
     dicti = {'1': 'one'}
     f(dicti)

#throwing error as "Print the exception one() missing 1 required positional argument: 'dicti'"

Please help me with this...

Hi @Akshay_15,

Why not post your question as a new topic instead of commenting on an old post here ?

class Callback:
    def __init__(self, fun, *args, **kwargs):
        self.fun = fun
        self.args = args
        self.kwargs = kwargs

    def __call__(self):
        self.fun(*self.args, **self.kwargs)

def say(message):
    print(message)


if __name__ == "__main__":
    callback = Callback(say, message="Hello")
    callback()

This would be more general solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.