George Sakkis a écrit : > On Nov 26, 2:04 pm, Bruno Desthuilliers > > <[EMAIL PROTECTED]> wrote: > >>Donn Ingle a écrit : >> >> >>>>I see someone already showed you eval. Eval is evil. Don't use it. >>>>Especially if the functions are coming to you from a public URL! >> >>>Yes, I suggested to him (by email) this: >> >>>thisinstance = SomeObject.__class__.__dict__ >>><Then you have a list of strings that may be function names, so:> >> >>This will only get attributes defined in SomeObject.__class__ - not the >>one defined in the parent classes. Nor methods dynamically bound to a >>specific instance. >> >> >> >> >>>for f in yourlist: >>> if f in thisinstance: eval(f)(params) >> >>>Which would vet the functions too. >> >>You *still* don't need eval here. >> >>target = <module or any other objet here> >>for funcname in funclist: >> func = getattr(target, funcname, None) >> if callable(func): >> func(*args, **kwargs) >> >>I've almost never had a real use case for eval (or exec) in 7 years. > > > That's not to say there aren't any.
Indeed. But most of the time, I found that Python provides a more appropriate solution. > In one project I'm using exec to > parse user provided function bodies from a web interface into real > python functions (the function signatures are auto generated). A problem I've never had to solve so far, so you're the expert here - but what about compiling the source with compile, then instanciating a function object from it ? (snip) -- http://mail.python.org/mailman/listinfo/python-list