Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function through a variable in Python

Tags:

python

I'm trying to make a game engine in Python as a "weekend project" but I have a problem.

I'm trying to make it that a user can declare a keybind by typing the key and the function into a text file what they want to run but when I run the code from that sheet with exec it runs the function and I have no idea on how to call the function through the variable. (Also I don't want it to run the function when it runs the code.)

Here is the code I use for executing the code from binds.zdata

for line in bind:
    try:
        exec line
    except:
        try:
            error.write(localtime + " : " + "ERROR: Could not bind key from the bind file : " + line)
        except:
            pass

Here's the text in binds.zdata

_w = Functions.motion.move().forward()
_a = Functions.motion.move().left()
_s = Functions.motion.move().back()
_d = Functions.motion.move().right()
like image 649
Zac Avatar asked Oct 11 '15 17:10

Zac


People also ask

How to call a function from a string in Python?

Another way to call a function from a string is by using the built-in functions locals () and globals. These two functions return a Python dictionary that represents the current symbol table of the given source code.

Why do we assign a function to a variable in 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.

How do you call a function from a variable?

To call the function you just need to write its name with proper arguments if needed. You can assign the return result to a variable and use it. check what we did in the dictify function.

How do functions work in Python?

How do Functions work in Python? Functions are the piece of code that is not executed automatically until you won’t call it. To call the function, just write the name of the function. Whenever a function is executed, a new symbol table is created internally in the memory.


2 Answers

You want to lose the () at the end of each line:

_w = Functions.motion.move().forward
_a = Functions.motion.move().left
_s = Functions.motion.move().back
_d = Functions.motion.move().right

now you can call the function through the variable by simply applying parenthesis, such as:

_w()
like image 158
Paul Evans Avatar answered Oct 21 '22 19:10

Paul Evans


I'm not sure I recommend "exec" since somebody could put any code in there.

But here is your problem. _w = Functions.motion.move().forward() Calls the function 'forward' and puts the results in _w. _w = Functions.motion.move().forward Assigns the function 'forward' to the variable '_w'.

Since you asked what I would do, I would create a set of tokens that represent the various functions and then let them do the mapping in side a config file (see: ConfigParser). Then parse the config file. It is a little more work, but a lot more secure.

like image 26
RobertB Avatar answered Oct 21 '22 19:10

RobertB