Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 python function question
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
coolgod




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
jcollins1991




PostPosted: 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:

Python:

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




PostPosted: 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?
jcollins1991




PostPosted: 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

Python:

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 Razz
coolgod




PostPosted: 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.
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
jcollins1991




PostPosted: 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:

Python:

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




PostPosted: 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:

Python:

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?
Sponsor
Sponsor
Sponsor
sponsor
jcollins1991




PostPosted: 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

Python:

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

Python:

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).
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: