Re: Given a string - execute a function by the same name

2008-05-08 Thread John Schroeder
You can do it with a class using the __getattr__ function. There might be a way to do it without a class but I don't how to do it that way. class AllMyFunctions(object): def a(self): print "Hello. I am a." def b(self): print "Hey. I'm b." x = raw_input("Enter a functio

Re: Given a string - execute a function by the same name

2008-05-08 Thread python
Andrew, > > I'm parsing a simple file and given a line's keyword, would like to call > > the equivalently named function. > No, actually, you wouldn't :-) Doing so means that if your programs input > specification ever changes, you have to rename all of the relevant functions. > Moreover, it

Re: Given a string - execute a function by the same name

2008-05-08 Thread Andrew Koenig
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > I'm parsing a simple file and given a line's keyword, would like to call > the equivalently named function. No, actually, you woudn't :-) Doing so means that if your programs input specification ever changes, you have to rename all

Re: Given a string - execute a function by the same name

2008-04-29 Thread python
Erik, > Perhaps I missed something earlier in the thread, but I really don't see the > need for that registry dict or the register decorator. Python already maintains a dictionary for each scope: The advantage of the decorator technique is that you explicitly declare which functions are eligible

Re: Given a string - execute a function by the same name

2008-04-29 Thread python
Arnaud, Just when I thought my solution couldn't get any better :) Thanks for that great tip and for an excellent demonstration of using a decorator. Regards, Malcolm You could avoid #5 from the start using a decorator: functions = {} def register(func): functions[func.__name__] = func

Re: Given a string - execute a function by the same name

2008-04-29 Thread python
Hi Max, Thank you for pointing out the pattern of my request. Using your google query (http://www.google.dk/search?hl=en&q=python+factory+pattern) I found the following description of what I'm doing. Command Dispatch Pattern http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html#id26

Re: Given a string - execute a function by the same name

2008-04-29 Thread Max M
[EMAIL PROTECTED] skrev: Bruno, But when I release into production I'm going to shift to #3: "Place all my functions in dictionary and lookup the function to be called". This technique will allow me to precisely control the dynamic nature of my application. Just one tiny note: What you will

Re: Given a string - execute a function by the same name

2008-04-29 Thread Arnaud Delobelle
On 29 Apr, 13:10, [EMAIL PROTECTED] wrote: > Bruno, > > Thank you for your detailed analysis. I learned a lot about Python > reading everyone's responses. > > For development I'm using #5: "globals().get("func")" because its > seamless to add additional functionality. > > But when I release into pr

Re: Given a string - execute a function by the same name

2008-04-29 Thread python
Bruno, Thank you for your detailed analysis. I learned a lot about Python reading everyone's responses. For development I'm using #5: "globals().get("func")" because its seamless to add additional functionality. But when I release into production I'm going to shift to #3: "Place all my functions

Re: Given a string - execute a function by the same name

2008-04-29 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : I'm parsing a simple file and given a line's keyword, would like to call the equivalently named function. There are 3 ways I can think to do this (other than a long if/elif construct): 1. eval() 2. Convert my functions to methods and use getattr( myClass, "method"

Re: Given a string - execute a function by the same name

2008-04-28 Thread Max M
[EMAIL PROTECTED] skrev: I'm parsing a simple file and given a line's keyword, would like to call the equivalently named function. There are 3 ways I can think to do this (other than a long if/elif construct): 1. eval() 2. Convert my functions to methods and use getattr( myClass, "method" )

Re: Given a string - execute a function by the same name

2008-04-28 Thread Arnaud Delobelle
[EMAIL PROTECTED] writes: > I'm parsing a simple file and given a line's keyword, would like to call > the equivalently named function. > > There are 3 ways I can think to do this (other than a long if/elif > construct): > > 1. eval() > > 2. Convert my functions to methods and use getattr( myClas

Re: Given a string - execute a function by the same name

2008-04-28 Thread Gary Herron
[EMAIL PROTECTED] wrote: I'm parsing a simple file and given a line's keyword, would like to call the equivalently named function. There are 3 ways I can think to do this (other than a long if/elif construct): 1. eval() 2. Convert my functions to methods and use getattr( myClass, "method" )

Re: Given a string - execute a function by the same name

2008-04-28 Thread Matimus
On Apr 28, 9:33 am, [EMAIL PROTECTED] wrote: > I'm parsing a simple file and given a line's keyword, would like to call > the equivalently named function. > > There are 3 ways I can think to do this (other than a long if/elif > construct): > > 1. eval() > > 2. Convert my functions to methods and us

Re: Given a string - execute a function by the same name

2008-04-28 Thread marek . rocki
One solution may be to use globals(): >>> globals()['foo']() Regards, Marek -- http://mail.python.org/mailman/listinfo/python-list

Re: Given a string - execute a function by the same name

2008-04-28 Thread Robert Bossy
[EMAIL PROTECTED] wrote: I'm parsing a simple file and given a line's keyword, would like to call the equivalently named function. There are 3 ways I can think to do this (other than a long if/elif construct): 1. eval() 2. Convert my functions to methods and use getattr( myClass, "method" )

Given a string - execute a function by the same name

2008-04-28 Thread python
I'm parsing a simple file and given a line's keyword, would like to call the equivalently named function. There are 3 ways I can think to do this (other than a long if/elif construct): 1. eval() 2. Convert my functions to methods and use getattr( myClass, "method" ) 3. Place all my functions i