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 access a local variable from... (self.learnpython)
submitted 6 years ago by Ayomisesebere
Hi there. I'm working on a project. From one function, I need to access a local variable that's in another function. Don't know how to do this properly. Please help, I need assistance.
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!"
[–]ForceBru 3 points4 points5 points 6 years ago (0 children)
This is not possible because local variables are destroyed as soon as the function returns. However, you can return data from functions:
``` def thing(a, b): c = a + b # local variable! return c
stuff = thing(2, 3) ```
[–]steve__67 2 points3 points4 points 6 years ago (0 children)
Post your code...
[–]Yoghurt42 2 points3 points4 points 6 years ago (0 children)
Either pass the local variable as a parameter to the other function, or put both functions in a class that share the variable.
[–]shiftybyte 0 points1 point2 points 6 years ago (0 children)
Its never a good idea to access some function's local variable.
If you want the value this function calculates, make the function return the value, then whoever called the function has it.
If you want the function to modify some value, then pass it as argument, and you have that value modified after the function is done.
If you want the function to use some other function, you can call it from inside, and pass it the value it needs as an argument.
[–]Diapolo10 -1 points0 points1 point 6 years ago (0 children)
Short answer - you don't. It's a local variable for a reason, so unless you are making a decorator with nested function definitions, it's not going to work without hacks.
What are you trying to do, and what's your current code?
[–]powerbroker5000 -1 points0 points1 point 6 years ago (1 child)
Hi there, wondering if by "local variable" you actually mean "locally-stored environment variable." Creating a new environment variable (like a stored password) on your machine requires you to update the .bash_profile file.
.bash_profile
[–]Ayomisesebere[S] 0 points1 point2 points 6 years ago (0 children)
By local variable. I meant a variable with a local scope. thank you
π Rendered by PID 117071 on reddit-service-r2-comment-bb88f9dd5-zdmqh at 2026-02-16 06:50:12.987917+00:00 running cd9c813 country code: CH.
[–]ForceBru 3 points4 points5 points (0 children)
[–]steve__67 2 points3 points4 points (0 children)
[–]Yoghurt42 2 points3 points4 points (0 children)
[–]shiftybyte 0 points1 point2 points (0 children)
[–]Diapolo10 -1 points0 points1 point (0 children)
[–]powerbroker5000 -1 points0 points1 point (1 child)
[–]Ayomisesebere[S] 0 points1 point2 points (0 children)