From dotted name to object

It is sometimes useful to be in position to select an arbitrary Python global object from a config file or some command line option. Things like this are called "dotted names".

That helper is a small undocumented treasure hidden in the stdlib pydoc module called locate.

>>> import pydoc
>>> MY_PI = pydoc.locate('math.pi')
>>> import math
>>> MY_PI
3.141592653589793
>>> MY_PI is math.pi
True

Enjoy...

Comments !

links

social