-4

I'm trying to change some variables (data frames) and created a simple example to illustrate.

First, I don't even know how to change the variables in the function. a is still 0 after the function completes.

Second, must I initialize a and b before calling the function if I want the function to set their value? or can I use them for the first time in my call?

def set_values(x,y):
    x,y=6,7
    return x,y

set_values(0,0)
Anup Yadav
  • 2,825
  • 3
  • 21
  • 30
Riley
  • 1
  • 1
    you are returning from function setValues but you are not saving it in a val like value = set_values(a,b) – Raman Mishra Jan 09 '19 at 10:01
  • 1
    Try `a,b = set_values()` – cdarke Jan 09 '19 at 10:02
  • Passing variables isn't a thing in Python. You're not passing the `a` and `b` variables, and you can't pass those variables. You're passing the values `0` and `0`. – user2357112 Jan 09 '19 at 10:03
  • If the function does not use/read `x` and `y`, you should just drop those parameters, and do `global a, b; a, b = 6,7` – tobias_k Jan 09 '19 at 10:03
  • 1
    @tobias_k: advising use of `global` without a reason is not a good idea. – cdarke Jan 09 '19 at 10:04
  • @cdarke Agreed, and the reason that I stated in my comment is that the method does not even use the parameters and OP seems to expect the method to `set` those values, which is what `global` would do. Still, it does probably not do what OP actually wants to achieve or expect, which is more a C thing than Python. – tobias_k Jan 09 '19 at 11:21

5 Answers5

2

First, you are correctly returning the values from your function, but if you want those values back in the calling code you have to do

a,b = set_values(a,b)

Otherwise the returned values just get thrown away.

For the second question, you don't have to initialize them exactly, but they have to exist before you reference them in the call to set_values(). But you don't have to pass them in because you are overwriting the values in any case:

def set_values():
    x,y=6,7
    return(x,y)

a, b = set_values()
BoarGules
  • 16,440
  • 2
  • 27
  • 44
0

This would set the values of a and b.

 a,b = set_values(a,b)

Explanation: set_values function is returning updated value of a and b, hence assign those return value to a,b respectively.

prashant
  • 2,808
  • 5
  • 26
  • 41
0
def set_values(x,y):
    x,y=6,7
    return(x,y)

a,b=0,0
values = set_values(a,b)

print(values)
NemoMeMeliorEst
  • 513
  • 4
  • 10
0
def set_values(x,y):
    x,y=6,7
    return(x,y)

a,b=0,0
a,b = set_values(a,b)

# a = 6 b =7

you are passing a values to a function. but you are not storing them in any variable( return value from function).

sahasrara62
  • 10,069
  • 3
  • 29
  • 44
0

You could set the variables with the return of the function but you should set with '=', something like:

a, b = set_values()

The variables inside of the function are local variables, you could use variables that are setted outside, using:

global my_var

and then, if you set inside, you could see the new values from outside. For example:

def my_function():
    global a
    a = "new value"

Like a good practice, its better use as less as possible, but you can.

Baurin Leza
  • 2,004
  • 1
  • 13
  • 15