What is the best way to use a function at the top of a module if do not know the name of the object in advance: The function might depend on user input and hence its name is stored in a variable.
One solution uses eval(): For example, in ScipyCookbook/SignalSmooth: smooth() (also referenced in an answer to StackOverflow: Python Smooth Time Series Data)
import numpy
window = 'hanning'
w = eval('numpy.'+window+'(11)')
will be equivalent to
w = numpy.hanning(11)
However, is there a better method than using fragile/potentially dangerous eval
?
For instance, wouldn't using vars()
w = vars(numpy)[window](11)
be preferred? Any better/more pythonic ideas?