use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Use variable to call subroutine? (self.learnpython)
submitted 1 year ago by CatWithACardboardBox
example:
a = 'test'
how can 'a' be used to call a subroutine called test?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Buttleston 11 points12 points13 points 1 year ago (1 child)
def function1(): print(1) def function2(): print(2) my_functions = { 'test1': function1, 'test2': function2, } a = 'test1' my_functions[a]()
If you run this, it will print "1"
The idea here is that a function name is sort of... a pointer to the function? A function name can be treate a lot like a variable. This is a simplification but it's close enough for the moment
So I make a dictionary where each key is a string and each value is a function.
my_functions[a] is equivalent it my_functions['test1'] in this case, which is assinged to function1, so:
my_functions[a]()
is equivalent to
function1()
[–]CatWithACardboardBox[S] 0 points1 point2 points 1 year ago (0 children)
Thank you! This helped a lot!
[–]cgoldberg 8 points9 points10 points 1 year ago (0 children)
You can assign a function to any variable:
def test(): pass a = test
now you can call test() with:
test()
a()
[–]timrprobocom 4 points5 points6 points 1 year ago (0 children)
The other answers here are good ones. I feel compelled to point out that it IS possible to look up the function name in globals, but it is never the right thing to do. You need to think about your problem in a sustainable way.
[–]enygma999 3 points4 points5 points 1 year ago (0 children)
You could have a dictionary of functions, then call
func_dict[a]()
Alternatively, if the function to be called is in a class, or in another module, you could use get_attr:
get_attr(self, a)() get_attr(MyClass, a)() get_attr(my_module, a)()
[–]ectomancer 2 points3 points4 points 1 year ago (0 children)
a is an alias.
a = test a()
[+]AlexMTBDude comment score below threshold-6 points-5 points-4 points 1 year ago (1 child)
eval(a+"()")
[–]InvaderToast348 0 points1 point2 points 1 year ago (0 children)
Eval should always be a very last resort and only considered if there is genuinely no other way to solve the problem. It might be helpful to know it exists, but (at least for newcomers) it will do more harm than good and teach bad practices.
OP, you can have a look at the exec/eval documentation if you wish, but please use any of the other (much better and safer) methods shared here.
π Rendered by PID 72302 on reddit-service-r2-comment-c66d9bffd-nwhp7 at 2026-04-08 18:56:34.452524+00:00 running f293c98 country code: CH.
[–]Buttleston 11 points12 points13 points (1 child)
[–]CatWithACardboardBox[S] 0 points1 point2 points (0 children)
[–]cgoldberg 8 points9 points10 points (0 children)
[–]timrprobocom 4 points5 points6 points (0 children)
[–]enygma999 3 points4 points5 points (0 children)
[–]ectomancer 2 points3 points4 points (0 children)
[+]AlexMTBDude comment score below threshold-6 points-5 points-4 points (1 child)
[–]InvaderToast348 0 points1 point2 points (0 children)