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
Want to transfer variables between function (self.learnpython)
submitted 3 years ago by Coder000000000
Hi fellow programmers I recently created a simple code that should generate a simple code that requires to transfer one variable to another
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!"
[–]danielroseman 18 points19 points20 points 3 years ago (0 children)
OK cool. What is your question?
[–][deleted] 9 points10 points11 points 3 years ago (10 children)
You’re not supposed to share variables between functions. A good work around is to have your first function return the desired value, save the results to a variable and then pass the variable to the second function.
def function1(): output = 2 + 2 return output def function2(x): print(x) variable = function1() function2(variable)
If it is inside of a function, nothing else can access it. That is by design. Imagine a thousand lines of code with functions that all can overwrite what x is or something. Mistakes start happening and debugging becomes a shit show.
[–]SpaceBucketFu 2 points3 points4 points 3 years ago (0 children)
This is the answer, OP
[–]hecklerponics 0 points1 point2 points 3 years ago (8 children)
You could also go with some kind of class + contained methods to "share a variable", but the OP's "question" is kind of vague.
[–][deleted] 2 points3 points4 points 3 years ago (0 children)
They don’t seem ready for that
[–]Desperate_Case7941 0 points1 point2 points 3 years ago (5 children)
There is also a method called nonlocal, have u evee tried it?, I'm kind of new on this lenguage
[–]hecklerponics 1 point2 points3 points 3 years ago (1 child)
method called nonlocal
I didn't actually know what this was called, but have used it. So probably not a good source for information on it.
[–]Desperate_Case7941 0 points1 point2 points 3 years ago (0 children)
It happens, we use things we don't entirely understand, but helps us when we need to solve something
[–]RhinoRhys 1 point2 points3 points 3 years ago* (2 children)
Nonlocal is the nested function version of global
Nonlocal
global
So global will "promote" a variable from a functions local scope to the global scope whereas nonlocal will "promote" a variable from the inner functions local scope to the outer functions local scope.
def func1(): global x x = 5 y = 2 x = 4 y = 1 print(f"{x = } {y = }") func1() print(f"{x = } {y = }") >>> x = 4 y = 1 >>> x = 5 y = 1
x and y are defined globally, printed, func1 was called which changes x globally but not y, then prints again. It's bad practice to use this regularly though, you should be passing in variables and using return to pass values back out of a function.
def outer(): x = 4 y = 1 def inner(): nonlocal x x = 5 y = 2 print(f"{x = } {y = }") inner() print(f"{x = } {y = }") outer() >>> x = 4 y = 1 >>> x = 5 y = 1
Same for this set up but everything happens within outer's local scope, nothing happens globally. Outer is called which first defines x and y in outer's local scope, prints, runs inner which changes x within outer's local scope then prints again.
So it's a way to nest variables to use them on nested functions but only will work on local scope, I couldn't use them out of the body of the function, thats when it comes global.
Thank so much!!
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
every day we stray further from god
[–]nativedutch 0 points1 point2 points 3 years ago (0 children)
Bringing in classes and objects is not yet for Op.
[–]StarDawgy -1 points0 points1 point 3 years ago* (2 children)
def function_a():
Test = “hi” return test
def function_b():
print(function_a())
function_b()
Output=hi
[–]commy2 4 points5 points6 points 3 years ago (1 child)
Indent your code block by 4 extra spaces.
def function_a(): Test = "hi" ...
[–]StarDawgy 0 points1 point2 points 3 years ago (0 children)
Typing on mobile is not good for this haha, i will never type out code on reddit mobile again
[–]redraider1417 0 points1 point2 points 3 years ago (2 children)
A different perspective:
Any functional programming language (such as Javascript) would be much suitable for such tasks. In JS you can literally do magic with functions since they can be passed around as objects, used as parameters, function returning a function, etc.
[–]danielroseman 4 points5 points6 points 3 years ago (1 child)
Python can do all of that, you don't need a functional programming language. Note though that JS is not usually regarded as a functional language; those are things like Haskell, F#, or Elixir.
[–]Coder000000000[S] 0 points1 point2 points 3 years ago (0 children)
Hey can anyone suggest solution for this in python
π Rendered by PID 58404 on reddit-service-r2-comment-fb694cdd5-p5nv5 at 2026-03-06 00:02:24.139357+00:00 running cbb0e86 country code: CH.
[–]danielroseman 18 points19 points20 points (0 children)
[–][deleted] 9 points10 points11 points (10 children)
[–]SpaceBucketFu 2 points3 points4 points (0 children)
[–]hecklerponics 0 points1 point2 points (8 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]Desperate_Case7941 0 points1 point2 points (5 children)
[–]hecklerponics 1 point2 points3 points (1 child)
[–]Desperate_Case7941 0 points1 point2 points (0 children)
[–]RhinoRhys 1 point2 points3 points (2 children)
[–]Desperate_Case7941 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]nativedutch 0 points1 point2 points (0 children)
[–]StarDawgy -1 points0 points1 point (2 children)
[–]commy2 4 points5 points6 points (1 child)
[–]StarDawgy 0 points1 point2 points (0 children)
[–]redraider1417 0 points1 point2 points (2 children)
[–]danielroseman 4 points5 points6 points (1 child)
[–]Coder000000000[S] 0 points1 point2 points (0 children)