简体   繁体   中英

How to run a python function

How do you run a python function. for instance if I have a file named test.py and a function inside like

def closest_to(l,v):
    num = l[0]
    diff_min = abs(l[0] - v)
    for i in xrange(1,len(l)):
        diff = abs(l[i] - v)
        if diff < diff_min:
            diff_min = diff
            num = l[i]
    return num

How would I test the function closest_to?

From: What does if __name__ == "__main__": do?

When your script is run by passing it as a command to the Python interpreter, python myscript.py all of the code that is at indentation level 0 gets executed. Functions and classes that are defined are, well, defined, but none of their code gets ran.

So, if the content of your script follows:

def closest_to(l,v):
    num = l[0]
    diff_min = abs(l[0] - v)
    for i in xrange(1,len(l)):
        diff = abs(l[i] - v)
        if diff < diff_min:
            diff_min = diff
            num = l[i]
    return num

if __name__ == '__main__':
    val1 = 10
    val2 = 200

    print 'Closes to %s, %s =' % val1, val2,
    print closest_to(val1, val2)

when you run

$ python script.py

It will call and output the result of your function. Alternatively I try to use doctests, If I want to try small methods, it's easier to manage.

For example:

def mysum(*args):
    """Returns the sum of all given arguments

    >>> mysum(1,2,3,4)
    10
    >>> mysum(1,2)
    3
    >>> mysum(1, -1)
    0
    >>> mysum(1)
    1
    """
    return sum(*args)

if __name__ == "__main__":
    import doctest
    doctest.testmod()

Run it and give it a try:

$ python mysum_script.py

Anything outside the function in global scope will be executed as part of the script:

def closest_to(l,v):
    num = l[0]
    diff_min = abs(l[0] - v)
    for i in xrange(1,len(l)):
        diff = abs(l[i] - v)
        if diff < diff_min:
            diff_min = diff
            num = l[i]
    return num

result = closest_to(val1, val2)
print result

If you'd like to have test.py with only the function definitions and would like to invoke these functions from another file, it is possible by importing test.py from the file where you need to use any of the functions.

# Some-other-file.py
import test
result = test.closest_to(val1, val2)
print result

If test.py contains a lot of functions and you know you're going to use only a few of them, you could import these specific few.

# Another-file.py
from test import closest_to, farthest_to
result = closest_to(val1, val2)
print result
farthest_to(val1, val2)

I've made an assumption that the function farthest_to does not have any return value and so not trying to store or print it. If you try to store/print such a value, you'd get none .

I would start up the python interpreter and import the file. Then you can test all you like :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM