back

by Panoramix·15y ago·view on hn ↗
It can be somewhat handy when working in interactive mode, though it has a very informal feel to it.

One (bad) example would go along the lines of:

>>> def f(x): ... if x in dir(scipy): ... print eval("scipy." + x + "(2)") ... >>> f("sin") 0.909297426826

Then you can let the user plot whatever function they want.

2 comments
Can't you use scipy.__dict__ to look up the function name in this case?

  scipy.__dict__[x](2)
It would be preferable to use getattr(scipy, x) rather than reaching inside the internals of the object (module).

    getattr(scipy, x)(2)