Global name error when importing a function
Question: Trying to kill two birds with one stone I decided to write a bit of code that
would let me practice python and calculus at the same time. I have two
seperate files, Derivative.py and newton_method.py (I know I should get a bit
better about naming my files correctly). The text from Derivatibe.py is as
follows

    
    
    def fx(value, function):
        x = value
        return eval(function)
    
    def function_input(input):
        function = str(input)
        return function
    
    def derivative_formula(x, h):
        return (fx(x - h, input_function) - fx(x + h, input_function)) / (2.0 * h)
    
    def derivative_of_x(x):
        error = 0.0001
        h = 0.1
        V = derivative_formula(x, h)
        print V
        h = h / 2.0
        derivative_estimate = derivative_formula(x, h)
        while abs(derivative_estimate - V) < error:
            V = derivative_formula(x, h)
            h = h / 2.0
            derivative_estimate = derivative_formula(x, h)
            print derivative_estimate
        return derivative_estimate
    

And the text from newton_method.py is:

    
    
    from Derivative import *
    
    input_function = function_input(raw_input('enter a function with correct python syntax'))
    
    E = 1 * (10 ** -10)
    
    guessx = float(raw_input('Enter an estimate'))
    
    def newton_method(guessx, E, function):
        x1 = guessx
        x2 = x1 - (fx(x1, input_function) / derivative_of_x(x1))
        while x2 - x1 < E:
            x1 = x2
            x2 = x1 - (fx(x1, input_function) / derivative_of_x(x1))
        return x2
    
    print "The root of that function is %f" % newton_method(guessx, E, input_function)
    

The error:

    
    
    Traceback (most recent call last):
      File "newton_method.py", line 17, in <module>
        print "The root of that function is %f" % newton_method(guessx, E,         input_function)
      File "newton_method.py", line 11, in newton_method
        x2 = x1 - (fx(x1, input_function) / derivative_of_x(x1))
      File "C:\Users\159micarn\Desktop\Python\Derivative.py", line 15, in derivative_of_x
        V = derivative_formula(x, h)
      File "C:\Users\159micarn\Desktop\Python\Derivative.py", line 10, in derivative_formula
        return (fx(x - h, input_function) - fx(x + h, input_function)) / (2.0 * h)
    NameError: global name 'input_function' is not defined
    

Do I need to declare input_function in Derivative.py? I would have thought
that declaring it in newton_method.py would have been enough.


Answer: Modules do not have access to the namespaces of the modules that import them.
Imagine 10 modules importing `Derivative.py`. Which `input_function` would it
use? `input_function` is just and another value and should be an additional
input parameter to `derivative_formula`.

