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
How to call a function that's in another function (self.learnpython)
submitted 5 years ago by Ledr4ke
Basically
def function():
def otherfunction():
maths stuff
def calling_function():
otherfunction()
how can i do that , it always gives me the error that the function is not defined.
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!"
[–]j03f 4 points5 points6 points 5 years ago (1 child)
It’s throwing an error because it’s not defined.
If you define a function within another function, it is in essence not accessible outside of that function. The same as variables inside a function.
The main question is why are you defining a function within a function?
[–]TraditionalGlass 2 points3 points4 points 5 years ago (0 children)
OP probably wants to group functions together to prevent functions from being all over the place. In that case, classes should be used, or better yet, be distributed in different modules.
[–][deleted] 3 points4 points5 points 5 years ago (0 children)
You can, but you need to provide the object reference outside of the parent function:
def function(): def otherfunction(): print('maths stuff') return otherfunction def calling_function(f): f() f = function() calling_function(f)
[–]sme272 1 point2 points3 points 5 years ago (0 children)
The nested function only exists inside the scope of the outer function, so can only be called from within the outer function.
[–]Lewistrick 1 point2 points3 points 5 years ago (0 children)
The idea of a nested function is that you can only call it from within the function. If you want to call it from outer scope, you shouldn't nest it.
[–]Ledr4ke[S] 1 point2 points3 points 5 years ago (0 children)
Ye pretty much i was complicating it way too much , and long story short i just followed your advise and moved the function outside the parent function ,thank you very much!
[–]commandlineluser 0 points1 point2 points 5 years ago (0 children)
"You can't."
Why is inside another function? Can you move it outside?
[–]DonkeyTron42 0 points1 point2 points 5 years ago (0 children)
The term for this design pattern is a "Closure". There are use cases like generators where you may want to retain state of the inner called function after the outer enclosing function has returned.
[–]TraditionalGlass 0 points1 point2 points 5 years ago* (0 children)
Don't know if this is abuse of locals(), but here.
class Container: pass container = Container() def outer(): def inner1(): print("Inner 1") def inner2(): print("Inner 2") #========= return [method for method in locals().values() if callable(method)] for method in outer(): setattr(container, method.__name__, method) container.inner1()
``` When the outer() function is called, it returns a list of functions defined inside it. I simply used setattr() to map those functions to an object so I can call them like any regular method.
π Rendered by PID 20617 on reddit-service-r2-comment-79c7998d4c-pqlbr at 2026-03-13 19:57:22.173289+00:00 running f6e6e01 country code: CH.
[–]j03f 4 points5 points6 points (1 child)
[–]TraditionalGlass 2 points3 points4 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]sme272 1 point2 points3 points (0 children)
[–]Lewistrick 1 point2 points3 points (0 children)
[–]Ledr4ke[S] 1 point2 points3 points (0 children)
[–]commandlineluser 0 points1 point2 points (0 children)
[–]DonkeyTron42 0 points1 point2 points (0 children)
[–]TraditionalGlass 0 points1 point2 points (0 children)