0

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?

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Jamie Nash
  • 19
  • 2
  • 4

3 Answers3

3

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
Community
  • 1
  • 1
Mario César
  • 3,699
  • 2
  • 27
  • 42
2

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.

Tuxdude
  • 47,485
  • 15
  • 109
  • 110
  • ?ok so I just need to call the function in the global section in the file? I can't call it outside the file or directly from the command line without implementing it inside the file? – Jamie Nash Feb 27 '13 at 03:27
  • I've updated my answer with few other alternatives which I believe is what you're looking for. – Tuxdude Feb 27 '13 at 03:34
  • ok so I did that with closest_to([1, 2, 3], 3) but then it gave me an error 'closest_to is not defined' – Jamie Nash Feb 27 '13 at 03:35
  • Yes you can invoke print if the function returns a value. If the function does not have a return value, the call to print would just print "none". Since closest_to in your question returns a value, I've updated my answer to store the value in another variable and then print it. – Tuxdude Feb 27 '13 at 03:39
  • @JamieNash which of the versions above gave you the `not defined` error? – askewchan Feb 27 '13 at 03:40
  • the one where i went into python mode in command promp first and then entered import file and ran that function. – Jamie Nash Feb 27 '13 at 05:52
-2

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

zanegray
  • 768
  • 7
  • 13