2

Before you claim as a duplicate I have looked at the following:
python function call with variable

Calling a function of a module from a string with the function's name

Executing a function by variable name in Python

https://ubuntuforums.org/showthread.php?t=1110989

and much more, but no luck.


I am developing a game. Right now I am working on the SAVE/LOAD feature, but I am getting nowhere.

I want to take a piece of text from another py or txt file and have my main py file read that, and call a function depending on the string of text in the second file.


Script1:

#imports

from script2 import SaveCode

#Code

def Test():
    print('Hello, World!')

callable(SaveCode)

Script2:

SaveCode = Test()

This won't work. Can someone please help me?

viddik13
  • 5,930
  • 2
  • 14
  • 21
Chase Barnes
  • 21
  • 1
  • 5
  • Please add traceback. – scharette Oct 24 '17 at 15:38
  • @scharette there is no traceback. It won't display anything. It doesn't call anything, it just exits, like to program is done. – Chase Barnes Oct 24 '17 at 15:39
  • What output do you expect? – cdarke Oct 24 '17 at 15:40
  • @cdarke It is supposed to call the `Test()` function, but it doesn't do it – Chase Barnes Oct 24 '17 at 15:41
  • Where is `callable` defined? – Kevin Oct 24 '17 at 15:42
  • 1
    `callable` is just a test if something could be called. It's a built-in. – VPfB Oct 24 '17 at 15:42
  • What should I use to call the Test() from another file? – Chase Barnes Oct 24 '17 at 15:43
  • 1
    [Callable()](https://docs.python.org/3.6/library/functions.html#callable) returns True or False. It does not execute the function. – Maurice Meyer Oct 24 '17 at 15:43
  • 1
    There _is_ a way to tell Python to execute the `Test()` function via the string `'Test()'`, but it's really _not_ a good idea to organize your code that way when there are cleaner, safer alternatives. – PM 2Ring Oct 24 '17 at 15:45
  • `retn = eval(SaveCode)` will work, but (see other comments) it is dangerous. Why do you need two modules for this? – cdarke Oct 24 '17 at 15:49
  • Because I don't know how to edit the contents of a python file using the same file – Chase Barnes Oct 24 '17 at 15:51
  • You don't need to edit it, you can choose different functions by assigning a function to a variable, for example: `runit = Func1` or `runit = Func2` then just call `runit()`. The dictionary method in @Bahrom's answer is a good way of doing it (called a *dispatch table*). – cdarke Oct 24 '17 at 15:54
  • Why do you want your script to edit a Python file? I thought you were just trying to add a simple SAVE / LOAD feature to your game. So you should be writing and reading data to a game save file. You shouldn't be modifying the Python code of some file! – PM 2Ring Oct 24 '17 at 15:55
  • yes, but when you exit a python file, they get set back to their defaults. IE. var1 = string, and in the program it changes it to string1. When you exit the program it changes back to string – Chase Barnes Oct 24 '17 at 15:56
  • @PM2Ring how would i do this? – Chase Barnes Oct 24 '17 at 15:56
  • 1
    You create a data file that has the data you want in it. When the program starts it reads the data so it can set your key variables to their initial values. And when you want to save the game you write the current values of those key variables to the file. If the data is simple you can just use a simple text file to do that. But if things are complicated you can use some kind of structured data file, like JSON. Another option is to use the `pickle` module which can save various Python objects and load them back in. – PM 2Ring Oct 24 '17 at 16:00
  • @PM2Ring oh. ok. – Chase Barnes Oct 24 '17 at 16:02

1 Answers1

1

This isn't going to work unless you use evaluate that string using eval or exec which isn't recommended.

From what I gather you have a string in script2 and want to execute a function in script1 based on that string. What you could do is define a dictionary that contains the strings you have mapped to the functions you want called:

In script1:

#imports

from script2 import SaveCode

#Code

def Test():
    print('Hello, World!')

functions = {
    'Test': Test
}

# Assuming that SaveCode = 'Test' in your second script,
# look up the corresponding function
function_to_run = functions[SaveCode]
# and call it
function_to_run() # prints Hello, World!

In script2:

SaveCode = 'Test'
Bahrom
  • 4,752
  • 32
  • 41
  • Traceback (most recent call last): File "C:\csc1AB\PROJECTS_\TEST\script1.py", line 3, in from script2 import SaveCode File "C:\csc1AB\PROJECTS_\TEST\script2.py", line 1, in functions[SaveCode]() NameError: name 'functions' is not defined – Chase Barnes Oct 24 '17 at 15:47
  • You're missing the `functions = {...` line in the first script – Bahrom Oct 24 '17 at 15:49
  • i copied everything from your answer into the 2 scripts – Chase Barnes Oct 24 '17 at 15:50
  • Read the comments in the answer – Chase Barnes Oct 24 '17 at 15:53
  • 2
    @ChaseBarnes: I don't see anything in that answer which says you should replace both scripts. Anyway, that is not the intention. – cdarke Oct 24 '17 at 15:57
  • 1
    "# Assuming that SaveCode = 'Test' in your second script, look up the corresponding function # and call it" – Chase Barnes Oct 24 '17 at 15:58
  • I'll update my answer to make it clearer if there are still questions when I get back to my desk. – Bahrom Oct 24 '17 at 16:09
  • @ChaseBarnes the comment in the code just meant that I was assuming you had a variable in your script2 called `SaveCode` and that its value was `'Test'`. Anyway, I updated the code to show the contents of both script1 and script2. – Bahrom Oct 24 '17 at 17:14
  • @ChaseBarnes and..? Do you still get that exception? See my edited answer for contents of script1 and script2. – Bahrom Oct 24 '17 at 17:28