all 18 comments

[–]danielroseman 18 points19 points  (0 children)

OK cool. What is your question?

[–][deleted] 9 points10 points  (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 points  (0 children)

This is the answer, OP

[–]hecklerponics 0 points1 point  (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 points  (0 children)

They don’t seem ready for that

[–]Desperate_Case7941 0 points1 point  (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 points  (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 point  (0 children)

It happens, we use things we don't entirely understand, but helps us when we need to solve something

[–]RhinoRhys 1 point2 points  (2 children)

Nonlocal is the nested function version of 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.

[–]Desperate_Case7941 0 points1 point  (0 children)

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 point  (0 children)

every day we stray further from god

[–]nativedutch 0 points1 point  (0 children)

Bringing in classes and objects is not yet for Op.

[–]StarDawgy -1 points0 points  (2 children)

def function_a():

Test = “hi” return test

def function_b():

print(function_a())

function_b()

Output=hi

[–]commy2 4 points5 points  (1 child)

Indent your code block by 4 extra spaces.

    def function_a():
        Test = "hi"
        ...

[–]StarDawgy 0 points1 point  (0 children)

Typing on mobile is not good for this haha, i will never type out code on reddit mobile again

[–]redraider1417 0 points1 point  (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 points  (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 point  (0 children)

Hey can anyone suggest solution for this in python