0

Suppose you have the following code to define this simple function:

def a(x,y,b):

    z=[x,y]

    c=b*2

    return c

I want to call this function with the value z[0] as input for parameter b. If I try to call the function in the following way:

a(x,y,z[0])

I get an error since the variable 'z' is defined inside the function and does not exists until the function runs.

I thought that when a function is called, python would simply replace the inserted inputs with the relative parameter inside the function to perform the desired calculation, independently from the pre-existence of an input variable. I mean, in my function for example, if I insert as input z[0] I would expect that python simply takes the digits z[0] and copy them in the function in place of b and perform the multiplication by taking the first element of the array z.

I make a step by step example with the desired output of the function to clarify the question: after defyining the above described function, I call it with these inputs

a(2,3,z[0])

here are the steps of execution of the function:

1) it computes the array z=[2,3]

2) it computes c=z[0]*2 ie c=2*2

3) it returns c=4

The step number 2) however doesn't takes place, since the variable z is created inside the function. I'm asking if there is a way to make the function "copy" the input digits z[0] inside the function in place of the parameter b during the execution of the function itself, so that python does not consider z[0] as a non existing variable, but as a simple piece of code to replace b with.

razorF
  • 11
  • 3
  • 1
    What you are asking for doesn't make sense, a parameter is a *name*, you have an expression that evaluates to some value, how would you *refer* to the parameter inside your function? It is not at all clear to me what you are expecting to happen. If you want `b` to equal `z[1]` then simply put `b = z[1]` inside your function – juanpa.arrivillaga May 08 '20 at 20:05
  • 1
    "In general is there a way to insert a varible created inside a function as function parameter ?" No, there isn't. That doesn't even make sense. Note, again, `z[1]` isn't even a variable, it is *an expression* – juanpa.arrivillaga May 08 '20 at 20:06
  • isnt z[1] just b couldn't you change `c=b*2` to `c=z[1]` which == `c=y*2` – Mason Caiby May 08 '20 at 20:12
  • I've made an update of the question to better clarify my problem – razorF May 08 '20 at 21:43
  • Please read [short-description-of-the-scoping-rules](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) - you need to have the variable you are passing to the function in scope when calling the function - those declared later inside the called function are not in scope – Patrick Artner May 08 '20 at 22:00
  • 1
    Function parameters are not text substitutions. `b` is not replaced with the _literal text_ `z[0]`, it means take the _value of the expression_ `z[0]`. – chash May 08 '20 at 22:02
  • The caller of `a` shouldn't even *know* that `z` is the name of a variable used the body of `a`. – chepner May 08 '20 at 22:03
  • Thank you for your answers. Do you now if there is a way to write the function so that it replace b with the literal text `z[0]`? – razorF May 08 '20 at 22:08
  • 1
    Why on *earth* would you want to? – chepner May 08 '20 at 22:10
  • 1
    @razorF Can you back up and explain the underlying goal here. Why do you think this is necessary. What problem are you trying to solve? – chash May 08 '20 at 22:14
  • @chepner I now that my request could sound strange, but I'm trying to apply this method to a more complicated code. I avoided to post my original code since it is very long to read, so I tried to make a simpler example. – razorF May 08 '20 at 22:16
  • 1
    This seems like a classic [XY problem](http://xyproblem.info). – chepner May 08 '20 at 22:20
  • This is a Bad Method. Whatever code you try to apply it to is going to be made *worse*. – chepner May 08 '20 at 22:23
  • ok, I thanks every one for help. If you think this is a bad method then i will try to do it in another way. – razorF May 08 '20 at 22:30

1 Answers1

0

This is horrible, but I'm starting to think it's what you want. Don't do this though. Really.

def a(x,y,b):
    z=[x,y]
    c=eval(b)*2
    return c

r = a(2,3,'z[0]')
print(r)

If you're curious why this works, it's delaying the evaluation of the expression z[0] until after z is defined inside the function's scope, rather than failing to evaluate it in the caller's scope.

chash
  • 3,975
  • 13
  • 29