Dynamically create class attributes

Posted by ahojnnes on Stack Overflow See other posts from Stack Overflow or by ahojnnes
Published on 2010-04-06T08:49:58Z Indexed on 2010/04/06 8:53 UTC
Read the original article Hit count: 214

Filed under:

Hi,

I need to dynamically create class attributes from a DEFAULTS dictionary.

defaults = {
    'default_value1':True,
    'default_value2':True,
    'default_value3':True,
}

class Settings(object):
    default_value1 = some_complex_init_function(defaults[default_value1], ...)
    default_value2 = some_complex_init_function(defaults[default_value2], ...)
    default_value3 = some_complex_init_function(defaults[default_value3], ...)

I could also achive this by having sth. like __init__ for class creation, in order to dynamically create these attributes from dictionary and save a lot of code and stupid work.

How would you do this?

Thank you very much in advance!

© Stack Overflow or respective owner

Related posts about python