-2

in python function,

def f(value):
    value = ~~~
    a = ~~~~

    return value, a

print(f(value))

I want to return value and a so the program out of the function also can memorize value and a, but want to show only a.

is there any method to process this?

  • I cannot use global, because the error says that 'value is parameter and global'
  • I must not change value in f() ('that means, i must not change f(value)')
  • Also, I must not change 'print(f(value))'. only thing i can do is changing inner of 'def f(value):'.
Jun
  • 9
  • 2
  • 2
    Show the full traceback of the error as properly formatted text in the question. Also format the code properly. – Michael Butscher Nov 08 '20 at 05:41
  • Try `print(f(value)[1])` – Mike67 Nov 08 '20 at 05:43
  • you can use list of length 2 to return the 2 values. – Mehr Muhammad Hamza Nov 08 '20 at 05:43
  • 1
    "I want to return value and a so the program out of the function also can memorize value and a, but want to show only a." If you had the two values in a tuple, how would you print only one of them? That's what you do. The fact that you got your result from a function call does not matter. "only thing i can do is changing inner of 'def f(value):'." This is incoherent. `print(f(value))` means to print the thing that `f(value)` returned, no matter what you put in `f`. If it returns a tuple ("two values"), you will print a tuple. – Karl Knechtel Nov 08 '20 at 05:57
  • 1
    Basically, your restrictions seem to be artificial and it's unclear why you want to do this. – Karl Knechtel Nov 08 '20 at 05:57
  • @Jun In [my answer](https://stackoverflow.com/a/64735308/941531) I provided solution without using any global variables (Second Variant) as you were asking specifically not to use globals plus provided simple solution with global variable just for reference of how it can also be solved (First Variant). – Arty Nov 08 '20 at 06:30

4 Answers4

1

You should assign value and a to variables outside of the function. Since you are returning a tuple you should assign it like x,y=f(value). Then print(y) to just show a.

James Tollefson
  • 883
  • 3
  • 14
Tyler F
  • 11
  • 1
0

As Karl Knechtel mentioned your requirements are inconsistent but the nearest you can get is:

def f(value):
    global other_value

    value = ~~~
    a = ~~~~
    other_value = value
    return a

print(f(value))

But use of global variables is not recommended.

Michael Butscher
  • 10,028
  • 4
  • 24
  • 25
0

In next two solutions I expect that you are allowed to change definition of your f(...) function. First solution uses global variable, and second doesn't use any globals.

First variant (below) is that you can just use a global variable to store saved value.

Try it online!

def f(value):
    global saved_value
    saved_value = value
    value = 123
    a = 456
    return a

# Regular usage of your function
value = 789
print(f(value)) # Prints 456

# Somewhere later in your code
# This will print saved value
print(saved_value) # Prints 789

Output:

456
789

Second variant (below) without using any global variable is by using state dictionary with default {} value. This state does same thing as global variable, it saves value for later use. I changed number of arguments in your function but that is not a problem because as before everyone can call your function just as f(value), the rest of arguments are not necessary to be provided and will be considered to be equal to default values.

Try it online!

def f(value, *, state = {}, return_value = False):
    if return_value:
        return state['value']
    else:
        state['value'] = value
    value = 123
    a = 456
    return a

# Regular usage of your function
value = 789
print(f(value)) # Prints 456

# Somewhere later in your code
# This will print saved value
print(f(None, return_value = True)) # Prints 789

Output:

456
789
Arty
  • 14,883
  • 6
  • 36
  • 69
-1

https://stackoverflow.com/a/45972642/9184997

def test():
    r1 = 1
    r2 = 2
    r3 = 3
    return r1, r2, r3

x,y,z = test()
print x
print y
print z


> test.py 
1
2
3