class checking its own module for an attribute

Peter Otten __peter__ at web.de
Wed Mar 21 17:59:56 CET 2012


Rod Person wrote:

> We have a module called constants.py, which contains [whatever] related to
> server names, databases, service account users and their passwords.

Passwords?

> In order to be able to use constants as command line parameters for
> calling from our batch files I created the class below that checks to
> make sure the parameter value is a valid constant and if so return its
> value.

Instead of

> from CCBH import constants
...
> if not hasattr(constants, value):
    ...

You can look up the name in the global namespace:

if value not in globals():
    ...





More information about the Python-list mailing list