0

I have two functions, and I need the variable I calculate in the first one for the calculation in the second function. My minimal working example is

import numpy.ma as ma
import numpy as np

data_1 = [0,2,4,6,8,10,12,14,16,18] 
error_1 = [0,1,2,3,4,5,6,7,8,9]

def mask(data,error): 
      product = np.multiply(data,error)

def plot(perc):
    result = ma.masked_array(product, product >= (np.percentile(product, perc)))

mask(data_1,error_1)
plot(30)
plot(60)
plot(90)

This gives me the error

NameError: name 'product' is not defined

I know that 'product' is local. Let's pretend it takes up a lot of computing time to multiply both lists because of high resolution and whatnot. That's why I want to put it in a separate function. Of course I could put everything in one function like this:

import numpy.ma as ma
import numpy as np

data_1 = [0,2,4,6,8,10,12,14,16,18] 
error_1 = [0,1,2,3,4,5,6,7,8,9]

def mask(data,error,perc): 
    product = np.multiply(data,error)
    result = ma.masked_array(product, product >= (np.percentile(product, perc)))

mask(data_1,error_1,30) mask(data_1,error_1,60) mask(data_1,error_1,90)

but with this I would calculate the product three times and it would take forever for the script to run. Is it clearer now what my problem is?

4
  • 1
    Because the x in second function is not defined and you are trying to do x+1! Commented Jul 5, 2018 at 10:37
  • Yes, but is there a way to combine both functions? In the original, the first function needs a lot of computing time and needs to computed only once. If I were to include in the second function, it would repeatedly be computed although it is not necessary Commented Jul 5, 2018 at 10:38
  • 4
    It seems like your minimal working example doesn't represent what you're trying to do in your real code. As it is, it just creates (or tries to create) local variables that don't get used for anything. Maybe you should give us something closer to your real code. Commented Jul 5, 2018 at 10:40
  • Possible duplicate of Using return value inside another function Commented Jul 5, 2018 at 10:44

2 Answers 2

0
  1. You passed the value 3 in the first function but you didn't use it (I mean the x=3)
  2. You made a calculation in the first function but you didn't return the result. The value of the x is passed by value, so it doesn't change oustide the function, it's just locally.
  3. You have to pass the value x in the second function

    def mask(y):
        return y ** 2
    
    def plot(z, x):
        z = x + 1
        print(z)
    
    x = mask(3)
    plot(3, x)
    

The result of this code is 10.

Alternative: you can pass the value by reference or you can use globale variables..

Sign up to request clarification or add additional context in comments.

Comments

0

Change your function so they actually return values instead of assigning to local variables (which are destroyed once the function exits anyway):

def mask(y):
    return y ** 2

def plot(x, z):
    # do something with x here

x = mask(3) 
plot(x, whatever)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.