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!
You could do it without metaclasses using decorators. This way is a bit more clear IMO:
Prior to Python 2.6 class decorators were unavailable. So you can write:
in older versions of python.
In the provided example
apply_defaults
is reusable… Well, except that the defaults are hard-coded in the decorator's body :) If you have just a single case you can even simplify your code to this:This is possible since classes (in the sense of types) are objects themselves in Python.
I think that's case for metaclass:
When defining a class, the local namespace will be converted into the class namespace at the conclusion of the class body. As such, you can accomplish this with: