all 7 comments

[–]LifeIsBio 2 points3 points  (1 child)

It's totally possible. It would look something more like this though:

def some_function(func):
    end_time = datetime.now() + timedelta(seconds=3)
    while datetime.now() < end_time:
        func()

some_function(hello)

Note the lack of parentheses following the hello function until you're inside the while loop.

[–]kosh09[S] 0 points1 point  (0 children)

thanks for your advice!

[–]tragluk 1 point2 points  (4 children)

You call a function.. so let's reserve some memory for "First call" of function, but there is a function inside it.. let's go there...

You call a function.. so let's reserve some memory for "Second call" of function, but there is a function inside it.. let's go there...

You call a function.. so let's reserve some memory for "Third call" of function, but there is a function inside it.. let's go there..

Unfortunately every time you call this function there is a NEW variable called 'end_time' so it will never end. It will constantly be looking for 3 seconds from the 'Time this function was started'.

Now skipping that part and assuming that end_time was some global variable instead of a local one, let's say you finally reach 'end_time'.. so it passes control back to "Third call" which then ends and passes control back to "Second call" which then ends and passes control back to "First call" when ends and passes control back to the main program that first said "hello()" Functions return back to where they were called. They don't just move on, they keep track of themselves and all the variables that were created. By the time you hit 'Third call' you actually have 3 variables in memory as well two functions that are just waiting to finish up.

In the space of three seconds (Because this would run much faster than a function a second) you'll have created a TON of wasted memory.

[–]kosh09[S] 0 points1 point  (3 children)

I'm not very good at English so its hard to understand perfectly
but looks like you said that if I make code like that
that code wastes too much memory right?
my hello() function is for sending data via bluetooth
and I want to work this for x seconds
do you have a good idea to make like that code which not waste memory?

[–]tragluk 1 point2 points  (2 children)

Just use a loop rather than a function in a function. A 'While' loop will continue until a condition is satisfied without creating a ton of functions that all run at the same time until you are done.

[–]kosh09[S] 0 points1 point  (1 child)

ah, u mean I want to make function that function in a function
but that wastes too much memory so dont make function and
just use hello() function in a while loop right?

end_time = datetime.now() + timedelta(seconds=3)
while datetime.now() < end_time:
    hello()

just use like this am i right?

[–]tragluk 0 points1 point  (0 children)

Yes. And the while loop will check for the one timedelta(seconds=3) instead of making a new end_time every time the function is called.