Computer Science Canada python function question |
Author: | coolgod [ Tue Dec 28, 2010 6:50 pm ] |
Post subject: | python function question |
Just a question. say I have a program that keeps on switching between 10 functions. The next function it runs is determined dynamically in the previous function. Say it starts from f1. and f1 determines where it goes next. I don't want to make it full of ifs. I would like to make it efficient. Originally i thought about returning another function but wouldn't that slow down the program eventually since f1 never really ended? I think in C i would use a function pointer. Any idea what i should do in python? |
Author: | jcollins1991 [ Tue Dec 28, 2010 7:16 pm ] | ||
Post subject: | Re: python function question | ||
Hmmm interesting question, if Python has good tail recursion stuff implemented you should be able to just do "return f2()"... Otherwise, here's my way to avoid going too deep in the recursion:
Each of your functions would return the next function and store it in the func variable and thenrun in the next iteration of the loop. |
Author: | coolgod [ Tue Dec 28, 2010 7:38 pm ] |
Post subject: | Re: python function question |
so the function variable really is a function pointer? It stores the address of the function space. And when i add a () at the end of the function variable it calls the function? |
Author: | jcollins1991 [ Tue Dec 28, 2010 7:48 pm ] | ||
Post subject: | Re: python function question | ||
I'm not really sure how it all works internally with Python, but it's probably something like that. Also, if you want to pass variables to the functions you could try something like
with the same loop as a wrapper. First though you should try finding out if the tail recursion is efficient, it could save you a lot of trouble ![]() |
Author: | coolgod [ Tue Dec 28, 2010 8:24 pm ] |
Post subject: | Re: python function question |
tail calls can't go on forever in python. I tried. I think i know how to solve my problem using those function variables. |
Author: | Tony [ Tue Dec 28, 2010 8:59 pm ] |
Post subject: | RE:python function question |
I don't think Python implements tail-end call elimination; meaning you'd run out of stack space. |
Author: | jcollins1991 [ Tue Dec 28, 2010 9:14 pm ] | ||
Post subject: | Re: RE:python function question | ||
Tony @ Tue Dec 28, 2010 8:59 pm wrote: I don't think Python implements tail-end call elimination; meaning you'd run out of stack space.
*sigh* too bad... Then executing the returned functions will probably be the easiest thing to do... Also, if you want the end return value to be something other than a function:
Realized that it'll try to execute a value returned no matter what it is, changed so that it only executes functions... |
Author: | coolgod [ Tue Dec 28, 2010 10:21 pm ] | ||
Post subject: | Re: RE:python function question | ||
jcollins1991 @ Tue Dec 28, 2010 9:14 pm wrote: Tony @ Tue Dec 28, 2010 8:59 pm wrote: I don't think Python implements tail-end call elimination; meaning you'd run out of stack space.
*sigh* too bad... Then executing the returned functions will probably be the easiest thing to do... Also, if you want the end return value to be something other than a function:
Realized that it'll try to execute a value returned no matter what it is, changed so that it only executes functions... just out of curiosity what does the return lambda: f2(1) do? what does return lambda do? |
Author: | jcollins1991 [ Tue Dec 28, 2010 10:31 pm ] | ||||
Post subject: | Re: python function question | ||||
Because if you want to pass parameters to it normally just writing
would call the f2 function and you'd end up with the tail call problems. By returning it in a lambda like
you can return the function to be called later. Lambda is basically a way of writing an anonymous function that can be passed around. Then in the main loop you just execute the returned value (the lambda function or other return value). |