
-----------------------------------
coolgod
Tue Dec 28, 2010 6:50 pm

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?

-----------------------------------
jcollins1991
Tue Dec 28, 2010 7:16 pm

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:


def f1():
  return f2

def f2():
  return false

def main():
  func = f1

  while func:
    func = func()
    


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.

-----------------------------------
coolgod
Tue Dec 28, 2010 7:38 pm

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?

-----------------------------------
jcollins1991
Tue Dec 28, 2010 7:48 pm

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


def f1():
  return lambda: f2(1)

def f2(a):
  print a


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 :P

-----------------------------------
coolgod
Tue Dec 28, 2010 8:24 pm

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.

-----------------------------------
Tony
Tue Dec 28, 2010 8:59 pm

RE:python function question
-----------------------------------
I don't think Python implements tail-end call elimination; meaning you'd run out of stack space.

-----------------------------------
jcollins1991
Tue Dec 28, 2010 9:14 pm

Re: RE:python function question
-----------------------------------
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:


def f1(): 
  return lambda: f2(1) 

def f2(a): 
  return a

def main(): 
  func = f1 

  while func.__class__.__name__ == "function": 
    func = func()


Realized that it'll try to execute a value returned no matter what it is, changed so that it only executes functions...

-----------------------------------
coolgod
Tue Dec 28, 2010 10:21 pm

Re: RE:python function question
-----------------------------------
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:


def f1(): 
  return lambda: f2(1) 

def f2(a): 
  return a

def main(): 
  func = f1 

  while func.__class__.__name__ == "function": 
    func = func()


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?

-----------------------------------
jcollins1991
Tue Dec 28, 2010 10:31 pm

Re: python function question
-----------------------------------
Because if you want to pass parameters to it normally just writing


return f2(1)


would call the f2 function and you'd end up with the tail call problems. By returning it in a lambda like


return lambda: f2(1)


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).
