-1

How could I reach an element within a def function. I want to compute mean by using p which is inside of the def function.

import numpy as np 
k= np.array([1,2,3,4,5,6])
def func():
    p = k + 5
    l = k + 25
    
func()
mean = p + 10
mhawke
  • 84,695
  • 9
  • 117
  • 138
fire fireeyyy
  • 71
  • 1
  • 8
  • 1
    lol? how about you return the variable in that function ? – Tomek Jan 13 '21 at 05:21
  • There are other ways to [access a local variable from outside a function](https://stackoverflow.com/questions/19326004/access-a-function-variable-outside-the-function-without-using-global/19327712). – Anderson Green Jan 13 '21 at 05:21
  • Please take some time to study basic Python before doing anything else like involving numpy or similar libraries. – Some programmer dude Jan 13 '21 at 05:23
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). "Show me how to solve this coding problem?" is off-topic for Stack Overflow. First, you need to learn to use the basic techniques of your current tools. Work through a tutorial on functions; see how to transfer values into and out of the function. – Prune Jan 13 '21 at 05:32
  • 1
    @AndersonGreen you *definitely* shouldn't do that. That doesn't even actually access a local variable outside the function. It creates an attribute on an object which is being referred to by a global name. Which is just a confusing, round-about way of using mutable global state, a bad practice to begin with. Instead, the function *should* return the data that is required by the caller.. – juanpa.arrivillaga Jan 13 '21 at 05:43

3 Answers3

2

When functions are done executing, all references to the variables within the function are cleared and removed from the stack. To get the values in the function, you have to use the return keyword.

Example:

def test_return_string():
    return "Hello World"

my_var = test_return_string()  # "Hello world" is returned and stored in my_var
print(my_var)  # Prints "Hello world"

When you do not define a return statement for the function. The function will return None by default.

Do note that returning a function will end its execution.

TL;DR - To get the variable p, you would just have to return p at the end of the function.

vrevolverr
  • 339
  • 3
  • 6
0
import numpy as np 
k= np.array([1,2,3,4,5,6])
def func(k):
    p = k + 5
    l = k + 25
    return(p,l)
    
p,l= func(k)
mean = p + 10
Anirban Saha
  • 1,350
  • 2
  • 10
  • 38
0
import numpy as np 
k= np.array([1,2,3,4,5,6])
def func(k):
    p = k + 5
    l = k + 25
    return p
    

mean = func(k) + 10

Here, I tried to make the more compact example with the less changes relative to your post.

When you def a function, in general you want to pass a variable through as argument to make the output related to the variable. So you def func(variable):, here def func(k):. That was the first "mistake".

Then, if you want the function to return a result, you need the return statement. That was the second "mistake".

Finally, if you want to use what the function returns, you need either to store the result in a variable, either to use it "in-place".

func(k) does not print (excepted in the python shell) nor assigns a value to a variable. It is just running and that's it.

print(func(k)) shows the result in the terminal (for both script/python sheel), wheras p = func(k) assign what func(k) returns to variable p. Then you can use p to compute the mean as mean = p + 10. Or you can directly use func(k) instead, because it provides your mean calculation with the same value than p (cause p = func(k))

Finally, in your function computing l seems to be useless now. So maybe think about removing it or using it within.

Synthase
  • 5,849
  • 2
  • 12
  • 34
  • Can you give a brief explanation of what you did here? (I know it may seem obvious) – HackSlash Jan 13 '21 at 23:23
  • Hey! See my edit. If that is helpful, think about accepting the answer ;) (gray tick top left of my answer) – Synthase Jan 13 '21 at 23:45
  • looks like this question is closed so OP can't. Your answer came up in my review queue and I voted to not delete it. – HackSlash Jan 13 '21 at 23:47
  • You're sure? I am pretty sure I got some answers accepted even after the question was closed (but new answers can't be posted). But I may be wrong. Anyway, not much a problem ;) – Synthase Jan 13 '21 at 23:49