[Tutor] how to get the return value?

Alan Gauld alan.gauld at freenet.co.uk
Sun Mar 5 23:58:35 CET 2006


> To make a time lapse video I've been playing with the sched module.
> There is one problem I run into, in the code below, how do I get the
> returned value t from printtime into main?

This is one place where you really are best to use a global variable IMHO.
There are some clever tricks using classes and so on but its really much
simpler to just define a global which is visible to both main and 
printtime()

> import time
> from sched import scheduler
>
> class time_lapse(scheduler):
>    def time_lapse(self, start_time, stop_time, interval, priority,
> action, argument):
>        def lapse():
>            action(*argument)

One possible alternative is to store the return value gere as an attribute.
Then main can query the object.

> def printtime(strf=None):
>    t=time.time()
>    if strf:
>        print time.strftime("%Y%m%d_%H%M%S")
>    else:
>        print time.localtime()
>    return t

I'd keep t as a local but assign the final value to a global with
a clear and unambiguous name.

> def main():
>    schedule = time_lapse(time.time, time.sleep)
>    start=time.time()
>    stop=list(time.localtime(start))
>    stop[3]=stop[3]+2
>    stop=time.mktime(stop)
>
>    schedule.time_lapse(start,stop,7,0,printtime,(1,))
>
>    schedule.run()

However the real question I have is how you intend to
access that result value. Is it a single value at the end
you want or the cumulative set of values from each
scheduled call?

Alan G. 



More information about the Tutor mailing list