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
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
<[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
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
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
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
[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
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
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
[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"
[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" )
[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
[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" )
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
One solution may be to use globals():
>>> globals()['foo']()
Regards,
Marek
--
http://mail.python.org/mailman/listinfo/python-list
[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" )
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
17 matches
Mail list logo