Of Healthy Programming Practices

Question:

I need advice. I’m writing a calculator; I’ve got the basic functionality of it down, and have finally moved to having it process functions. In this regard, I find the standard library of mathematical functions provided by Python unsatisfactory.

While the more commonly used functions are defined, some of the more obscure functions lack proper mention. Examples of this include the arc hyperbolic cotangent, arc hyperbolic cosecant, hyperbolic cosecant, secant, etc. While some of these are just manipulations of the functions provided, it seems the better alternative would be to provide our definitions and refer the program to those when it encounters them.

This is where my question lies. Should I just define these functions in the main script, or should I use a separate file? If I the latter, how do I make reference to the separate file? I am relatively new to Python, so if I’m missing a common technique or something, please do let me know. Thanks in advance.

Asked By: ThisIsNotAnId

||

Answers:

Should I just define these functions in the main script, or should I use a separate file? If I the latter, how do I make reference to the separate file? I am relatively new to Python, so if I’m missing a common technique or something, please do let me know. Thanks in advance.

The import statement is an important part of python:

Put your functions in a separate file, say mymath.py:

def foo(x):
    # Code goes here

Then import your module:

import mymath
print(mymath.foo(y))
Answered By: sverre

I would make a small math utility module like you suggest. Simply create advmath.py with

def csch(z):
    "Hyperbolic cosecant"
    return 1 / sinh(z)

def sinh(z):
    "Hyperbolic sine"
    ...

and then you can import this module in your main script:

import advmath

You now have access to functions and variables defines in the module:

advmath.csch(x)
Answered By: Martin Geisler

You should get the numpy and scipy libraries which include many of these functions for you.

Should I just define these functions in the main script, or should I
use a separate file?

It depends on how complicated your calculator is. But it probably should go in a separate file.

If I the latter, how do I make reference to the separate file?

Use import. import foo will try to load the file foo.py. Then you access the functions inside foo as foo.function_name. You should be able to find a lot of questions about using it on this site.

Answered By: Winston Ewert

You should spend some time with the excellent tutorial regarding modules on the python site.

In general, yes. If you have components that you want to reuse, these are best kept in separate module files.

Example:

#!/usr/bin/python

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print b,
        a, b = b, a+b
                    
if __name__ == "__main__":
    print "testing fib(10)"
    fib(10)   

Place that code in a seperate file called fib.py in a known directory — say your personal bin folder. Run the python interactive shell from that directory.

Now:

>>> import fib
>>> fib.fib(10)
1 1 2 3 5 8

With this method you can build up a personally library of functions that is readily available to you.

Note the code here:

if __name__ == "__main__":
    print "testing fib(10)"
    fib(10)   

That allows you to place a test harness and the end of your modules that will execute if the module is executed other than as a library import.

Much of this may not be necessary however. The excellent numpy and scipy probably have every math function that you can dream of.

Answered By: the wolf
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.