return-value

Returning Variables in Functions Python Not Working Right

Returning Variables in Functions Python Not Working Right Question: I have been trying to return a variable in a function in a variable and use it outside of it: test = 0 def testing(): test = 1 return test testing() print(test) But when I run it, the result is 0. How could I fix this …

Total answers: 5

python function not returning a list

python function not returning a list Question: I can’t figure out why my implementation of Reverse Cuthill–McKee algorithm is returning at the wrong place. Basically, it produces the correct result, but when I run the code, it returns a different value. Here is a full code with a graph, could some one please explain this …

Total answers: 1

store return value of a Python script in a bash script

store return value of a Python script in a bash script Question: I want to execute a python script from a bash script, and I want to store the output of the python script in a variable. In my python script, I print some stuff to screen and at the end I return a string …

Total answers: 6

How can I return two values from a function in Python?

How can I return two values from a function in Python? Question: I would like to return two values from a function in two separate variables. For example: def select_choice(): loop = 1 row = 0 while loop == 1: print(”’Choose from the following options?: 1. Row 1 2. Row 2 3. Row 3”’) row …

Total answers: 8

How to get the return value from a thread in Python?

How to get the return value from a thread? Question: The function foo below returns a string ‘foo’. How can I get the value ‘foo’ which is returned from the thread’s target? from threading import Thread def foo(bar): print(‘hello {}’.format(bar)) return ‘foo’ thread = Thread(target=foo, args=(‘world!’,)) thread.start() return_value = thread.join() The "one obvious way to …

Total answers: 26

Is it ok to skip "return None"?

Is it ok to skip "return None"? Question: I wonder if it is bad manner to skip return None, when it is not needed. Example: def foo1(x): if [some condition]: return Baz(x) else: return None def foo2(x): if [some condition]: return Baz(x) bar1 = foo1(x) bar2 = foo2(x) In both cases, when condition is false, …

Total answers: 8

Returning None or a tuple and unpacking

Returning None or a tuple and unpacking Question: I am always annoyed by this fact: $ cat foo.py def foo(flag): if flag: return (1,2) else: return None first, second = foo(True) first, second = foo(False) $ python foo.py Traceback (most recent call last): File “foo.py”, line 8, in <module> first, second = foo(False) TypeError: ‘NoneType’ …

Total answers: 11

How to call an external program in python and retrieve the output and return code?

How to call an external program in python and retrieve the output and return code? Question: How can I call an external program with a python script and retrieve the output and return code? Asked By: cfischer || Source Answers: Look at the subprocess module: a simple example follows… from subprocess import Popen, PIPE process …

Total answers: 5

Is it pythonic for a function to return multiple values?

Is it pythonic for a function to return multiple values? Question: In python, you can have a function return multiple values. Here’s a contrived example: def divide(x, y): quotient = x/y remainder = x % y return quotient, remainder (q, r) = divide(22, 7) This seems very useful, but it looks like it can also …

Total answers: 9