0

I want to create a function where I can pass in specified variables into the function and return them out of the function as true.

Something along the lines of :

y = False
z = False

def func(x):
    {do stuff}
    x = True
    return x

func(y)
func(z)

So that I can pass in various different variables that aren't statically set in the function, adjust them to true and return the value out.

But I'm currently struggling with understanding how to do something like this.

Going about it this way its just trying to create a new variable called x and return it as opposed to use the variable in the parameters (x) and override that.

Limes
  • 11
  • 2
  • 2
    `bool` are immutable, so you would have to rebind those global variables. But function parameters are *always* local in scope. So this is not possible without some anti-pattern hacks like modifying the `globals()` dict after passing the variable names as strings. This rekes of an [xy-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – user2390182 Aug 22 '23 at 14:39
  • 2
    You can change your code to: `y = func(y)` and `z = func(z)`. – quamrana Aug 22 '23 at 14:41
  • 1
    Rather than trying to set various globals based on their name, you'd be better off keeping your values in a dictionary. – Tom Karzes Aug 22 '23 at 14:43
  • Does this answer your question? [How do I pass a variable by reference?](https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) – slothrop Aug 22 '23 at 17:19

4 Answers4

3

Try

def func(x):
    return True

y = False
y = func(y)
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • 1
    I like this answer as it illustrates the spuriousness of passing any parameter to the function when you assign the return value anyway. – user2390182 Aug 22 '23 at 14:45
1

The classic approach is to use an object that is passed by reference, e.g. a list, or dictionary:

state = dict(y=False, z=False)

def func(state, variable_name):
    # {do stuff}
    state[variable_name] = True
    return state[variable_name]

func(state, 'y')
func(state, 'z')

But I would suggest the approach without "side-effects", i.e. changing the variables. Instead try to return the new values and reassign like so.

y = False
z = False

def func(x):
    # {do stuff}
    x = True
    return x

y = func(y)
z = func(z)
Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
  • *"The classic approach is to use an object that is passed by reference"* I know what you mean, but the point is to use a *mutable* object. All Python objects, mutable and immutable, are passed to functions in the same way. – slothrop Aug 22 '23 at 15:13
0

This is a hack and an anti-pattern and surely whatever the underlying problem is that you are trying to solve, there is an approach that does it without the use of global variables:

x = y = False

def truthify(name):
    globals()[name] = True

truthify("x")
x
# True
y
# False
user2390182
  • 72,016
  • 6
  • 67
  • 89
0

what you want to do is to pass value by reference and change it inside your function. which is not possible in python because Python passes arguments neither by reference nor by value, but by assignment. in other words, variables in python can be classified in two groups, mutable and immutable. mutable objects are automatically passed by reference. immutable objects are automatically passed by value.

booleans are immutable so what you want is not possible, but you can try something like:

y = False
z = False

def func(x):
    {do stuff}
    x = True
    return x

y = func(y)
z = func(z) 
hamed danesh
  • 361
  • 9
  • What purpose has the `x = True` line exactly? – user2390182 Aug 22 '23 at 14:47
  • i assigns the true value function returns to variables. – hamed danesh Aug 22 '23 at 14:50
  • You could replace the two lines `x = True` and `return x` with just `return True`. – slothrop Aug 22 '23 at 15:02
  • *"immutable objects are automatically passed by value."* Immutable objects aren't passed by value, they're passed in just the same way as mutable objects (demonstration: https://ideone.com/I7H6dW) The function simply can't mutate an immutable object (obviously). – slothrop Aug 22 '23 at 15:04
  • well isn't result the same? – hamed danesh Aug 22 '23 at 15:38
  • For someone who is coming from a language with a distinction between pass-by-reference and pass-by-value, the heuristic "immutable objects behave as if they were passed by value" will lead them to expect the right behaviour in most cases. However, it's more satisfying IMO to understand how Python really works. And if you're not weighed down by baggage from other languages, it's simpler too - there's no need to learn a less accurate mental model which claims there are two types of parameter-passing, rather than a more accurate model with only one type. – slothrop Aug 22 '23 at 15:58