Cédric
Cédric

Reputation: 31

NameError: function name is not defined

I am programming a small Python module with Python 2.7.6 (default, Jun 22 2015, 17:58:13) and I am using IPython 1.2.1 for running my program.

This module will contain several mathematical function evaluations with power series. I am separating python function definitions from python function tests.

Here is my code for PowerSeries.py :

### Computations of some functions by power series ###

def exp(x):
    """Exponential function"""
    exp=1.0
    term=1.0
    iteration=1
    factorial=1
    while(abs(term/factorial)>1e-10):
        factorial*=iteration
        term*=x
        exp+=term/factorial
        iteration+=1
    return exp

def hyperbolicCosine(x):
    """Hyperbolic cosine function"""
    cosh=1.0
    term=1.0
    iteration=1
    factorial=1
    while(abs(term/factorial)>1e-10):
        factorial*=iteration
        term*=x
        if iteration%2==0:
            cosh+=term/factorial
        iteration+=1
   return cosh

def sine(x):
    """Sine function"""
    sine=0.0
    term=1.0
    iteration=1
    factorial=1
    while(abs(term/factorial)>1e-10):
        factorial*=iteration
        term*=x
        if iteration%2==1:
            sine+=term/factorial                
            term=-term
        iteration+=1
    return sine

def cosine(x):
    """Cosine function"""
    cosine=1.0
    term=1.0
    iteration=1
    factorial=1
    while(abs(term/factorial)>1e-10):
        factorial*=iteration
        term*=x
        if (iteration)%2==0:
            term=-term
            cosine+=term/factorial              
        iteration+=1
    return cosine

Here is my code for tests.py :

### Tests for PowerSeries Module ###
from math import pi
from PowerSeries import *

# Tests of exponential
print "### Tests of exponential ###"
x=-2
print("x=%f exp(x)=%.10f" % (x,exp(x)))
x=-1
print("x=%f exp(x)=%.10f" % (x,exp(x)))
x=0
print("x=%f exp(x)=%.10f" % (x,exp(x)))
x=1
print("x=%f exp(x)=%.10f" % (x,exp(x)))
x=2
print("x=%f exp(x)=%.10f" % (x,exp(x)))

# Tests of hyperbolic cosine
print "### Tests of hyperbolic cosine ###"
x=-2
print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))
x=-1
print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))
x=0
print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))
x=1
print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))
x=2
print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))


# Tests of sine
print "### Tests of sine ###"
x=0
print("x=%f sin(x)=%.10f" % (x,sine(x)))
x=pi/6
print("x=%f sin(x)=%.10f" % (x,sine(x)))
x=pi/4
print("x=%f sin(x)=%.10f" % (x,sine(x)))
x=pi/3
print("x=%f sin(x)=%.10f" % (x,sine(x)))
x=pi/2
print("x=%f sin(x)=%.10f" % (x,sine(x)))

# Tests of cosine
print "### Tests of cosine ###"
x=0
print("x=%f cos(x)=%.10f" % (x,cosine(x)))
x=pi/6
print("x=%f cos(x)=%.10f" % (x,cosine(x)))
x=pi/4
print("x=%f cos(x)=%.10f" % (x,cosine(x)))
x=pi/3
print("x=%f cos(x)=%.10f" % (x,cosine(x)))
x=pi/2
print("x=%f cos(x)=%.10f" % (x,cosine(x)))

Everything works fine until I add the hyperbolicCosine definition. Executing the tests.py file gives a NameError that I can't solve :

     19 print "### Tests of hyperbolic cosine ###"
     20 x=-2
---> 21 print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))
     22 x=-1
     23 print("x=%f cosh(x)=%.10f" % (x,hyperbolicCosine(x)))

NameError: name 'hyperbolicCosine' is not defined

Help is welcome.

Upvotes: 3

Views: 2202

Answers (1)

veggie1
veggie1

Reputation: 747

I can't comment because I am too new, but it's always is a better idea to explicitly import what you need from a particular module. If you always do that, it could help narrow down the problem in the future (i.e. you will get an ImportError instead of a NameError.)

Moreover, .pyc files are regenerated based on timestamp. It seems like this problem is more likely due to iPython having the old module in memory.

Upvotes: 1

Related Questions