1

The below script works fine, until I added in "def test", I am trying to get rid of all global variables my programming in general and as I'm not a great programmer, I hope there is a way to do this.

I want to pass "foo" from function "test" back to Function "work" but this does not work as its not a global variable. Any ideas?

bar = "bar"
barnone = "barnone"

def function_A():
    data = 5
    data1 = 15
    if host == 1:
        work(data, data1)
    else:
        function_B()

def function_B():
    data = 3
    data1 = 13
    work(data, data1)
    test(data)
    print foo

def work(data,data1):
    print data
    print data1
    test(data)
    print foo

def test(data):
    if data == 3:foo = bar
    elif data == 5:foo = barnone

if __name__ == '__main__':
    host = 11
    function_A()

EDIT:

Thank you, this works... I appreciate all the feedback as I am a novice, keep in mind this was just a test script I put together to understand passing parameters to different functions. Before this I was using globals and I'm trying to get rid of them.

Thank you, any advice is helpful.

bar = "bar"
barnone = "barnone"

def function_A():
    data = 5
    data1 = 15
    if host == 1:
        work(data, data1)
    else:
        function_B()

def function_B():
    data = 3
    data1 = 13
    work(data, data1)
    test(data)

def work(data,data1):
    print data
    print data1
    test(data)
    print test(data)

def test(data):
    if data == 3:foo = bar
    elif data == 5:foo = barnone
    return foo

if __name__ == '__main__':
    host = 11
    function_A()
1
  • 1
    Perhaps a Python tutorial will clear things up for you. Your code has so many issues that it's hard to even begin describing them. docs.python.org/3/tutorial Commented Aug 12, 2016 at 15:53

3 Answers 3

4

Add the following to the end of your test() function:

`return foo`

then you can print the variable in work() like this

print test(data)
Sign up to request clarification or add additional context in comments.

Comments

2

foo is only defined in the "scope" of the function test(), since that is where you created it. The function work() has no knowledge of the variable foo, as it is undefined outside of the function test(). So, test() has to return the variable foo to the place that called test(), which is the line test(data) in work().

So, yes, add return foo to the end of test().

Edit:

When you say test(data), that is basically saying sum([1,2,3]). You've called a function, but you're not doing anything with the result, you're not assigning it. You have to say new_variable = test(data). This means, from the perspective of work(): "call the function test() and have it do its thing. I don't care what's going on inside of test(), but I am expecting to it to spit something out at me when it is done. I will assign that something to a variable in my scope so I can use it later".

It is just like saying x = sum([1,2,3]). sum is a function that does something inside of it, you don't really care what, you just know that it should return a sensible value that you will assign to x to use later.

Edit2: Also, as it stands, test() is going to return a boolean for foo, since you use the == operator rather than the assignment operator =.

Comments

1

Your test function should be written like

def test(data):
    if data == 3:
        return 'bar'
    elif data == 5:
        return 'barnone'

In a function that calls test, assign the result

something = test(data)

Note that your code has some unrelated problems, e.g., what should happen if data is neither 3 nor 5?

5 Comments

OP is obviously a very novice programmer. I don't see any point in making stylistic suggestions which may just confuse him for now, such as moving the return statements around. Its easier just to point out that his code as it is in test is fine, and it just needs a return at the end. Edit: "fine" as in functional
@jphollowed For that reason, I didn't mention the ternary operator. I believe that at OP level, full control-path thought is necessary.
I agree, I just think it makes more sense for him to put a return statement at the bottom of the function, and understand what it does, before he worries about modifying his whole function. That may make it easier to understand precisely where the difference is being made.
@jphollowed Oh, I see. However, the point remains, no? The logic is undefined when the value is neither of the two. I was not trying to make a stylistic point of where the returns should go (if only because I don't have an opinion on it).
@ Ah yes you're right, sorry. Perhaps it would be good for OP to add a naive return and then interpret the error when data != 3 or 5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.