handle handle - 4 months ago 10
Python Question

Get attributes from module imported with star

A python script needs a per-user configuration to override (redefine) "default" assignments (e.g.

path = "local/path/"
) which could by done by importing a custom module's attributes with
from custom_settings import *
. I would like to output the ones that are being changed by the custom module.

From within
custom_settings
its attributes can be obtained with
dir()
, e.g.
", ".join( (a for a in dir() if not a.startswith("__") ) )
.
How can this be done from the importing script if the imported module attributes are bound to the existing namespace?

Answer

This may not be the neatest solution, and it seems painfully redundant, but you could always simply:

from custom_settings import *
import custom_settings
overridden = ", ".join(a for a in dir(custom_settings) if not a.startswith("__"))
Comments