1

In this example test_function1 has 4 varibles that need to be used in test_funtion2.I would not like using global variables becouse the actual code i'm writing is more complex and it would break it.

def test_function1():
    a = input("Type aaa:")
    b = "bbb"
    c = "ccc"
    d = "ddd"


test_funtion1()


def test_function2():
    if a == "aaa"
        print(b)
        print(c)
        print(d)


test_function2()

I have a solution, but I am not sure if it is good or not.Could you tell me if this would work or if there is any other alternative.Thanks! Sorry for my grammar , english is not my main language.

def test_function1():
    a = input("Type aaa:")
    b = "bbb"
    c = "ccc"
    d = "ddd"
    return (a, b, c, d)


def test_function2():
    if (test_funtion1()[0]) == "aaa"
        print(test_funtion1()[1])
        print(test_funtion1()[2])
        print(test_funtion1()[3])
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • You're calling `test_function1` 4 times instead of just storing its result once and inspecting it. What about doing `res = test_function1()` and then using `res[0]`, `res[1]` etc? – GPhilo Jun 11 '19 at 10:11
  • Do you _really_ need to call `test_function1` over and over again? I think you can call it once, save the result to a variable and then index the variable. – ForceBru Jun 11 '19 at 10:12
  • Oh I understand what you mean –  Jun 11 '19 at 10:13
  • That would be way better, thnaks! –  Jun 11 '19 at 10:14

4 Answers4

2

I think what you're looking for are classes.

a, b,c, d is your state, and an instantiation of such a class forms a state, which is basically the values referenced by these 4. Your first function is the "constructor" (called __init__) and the second function is then able to access these "instance variables".

Harald Schilly
  • 1,098
  • 1
  • 14
  • 15
  • Classes are a very useful tool indeed, but they are not needed here, and might be a bit too much of an advanced topic for the OP (IOW: mentionning classes is a good idea but that should only be as a bonus to the proper answer). (my two cents, IMHO etc). – bruno desthuilliers Jun 11 '19 at 10:53
1

I edited the solution that best fits me:

def test_function1():
    a = input("Type aaa:")
    b = "bbb"
    c = "ccc"
    d = "ddd"
    return a, b, c, d


def test_function2():
    x = test_function1()
    if x[0] == "aaa":
        print(x[1])
        print(x[2])
        print(x[3])


test_funtion2()
  • Then the comments on my question would not make any sense, so i wrote this answer instead. –  Jun 11 '19 at 11:07
  • The intention is good and I can only approve it, but you should _still_ have edited your answer, adding the fixed code (instead of replacing the original one) and mentionning the comment that led you to this edit ;-) – bruno desthuilliers Jun 11 '19 at 11:11
1

Passing arguments to functions and returning values from functions is indeed the first and most obvious way to avoid global state - but wrt/ to your snippet, you should avoid calling test_function1 four times, which is done by keeping the result in a local variable:

def test_function2():
    result = test_funtion1() 
    if result[0] == "aaa"
        print(result[1])
        print(result[2])
        print(result[3])

or in this specific case (when the function returns a tuple or ny sequence of known length) you could use tuple unpacking:

def test_function2():
    a, b, c, d = test_funtion1() 
    if a == "aaa"
        print(b)
        print(c)
        print(d)

Also, if you have a set of functions working on the same set of (related) variables, you may want to have a look at classes and objects.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • I already made an answer with the first solution , but thank you :) –  Jun 11 '19 at 11:03
1

You can also use this

def test_function1():
    a = input("Type aaa:")
    b = "bbb"
    c = "ccc"
    d = "ddd"

    t =  a, b, c, d
    return t


def test_function2():
    x = test_function1()
    if x[0] == "aaa":
        print(x[1])
        print(x[2])
        print(x[3])


test_function2()
Kalana
  • 5,631
  • 7
  • 30
  • 51