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?

1

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

?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?
I've updated my answer with few other alternatives which I believe is what you're looking for.
ok so I did that with closest_to([1, 2, 3], 3) but then it gave me an error 'closest_to is not defined'
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.
@JamieNash which of the versions above gave you the not defined error?
|
-2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.