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?

17
  • Please add traceback. Commented Oct 24, 2017 at 15:38
  • 1
    callable is just a test if something could be called. It's a built-in. Commented Oct 24, 2017 at 15:42
  • 1
    Callable() returns True or False. It does not execute the function. Commented Oct 24, 2017 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. Commented Oct 24, 2017 at 15:45
  • 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. Commented Oct 24, 2017 at 16:00

1 Answer 1

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

9 Comments

Traceback (most recent call last): File "C:\csc1AB\PROJECTS_\TEST\script1.py", line 3, in <module> from script2 import SaveCode File "C:\csc1AB\PROJECTS_\TEST\script2.py", line 1, in <module> functions[SaveCode]() NameError: name 'functions' is not defined
You're missing the functions = {... line in the first script
i copied everything from your answer into the 2 scripts
@ChaseBarnes: I don't see anything in that answer which says you should replace both scripts. Anyway, that is not the intention.
"# Assuming that SaveCode = 'Test' in your second script, look up the corresponding function # and call it"
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.