rec*_*gle 0 python attributes dictionary
我有一本大字典.我想将每个键值对分配给一个类的属性.目前我在下面的代码片段中有一些内容.它有效,但它并不漂亮.有一个更好的方法吗?
伪python片段:
config = A large dictionary
self.Width = config['width']
self.Height = config['height']
self.Text = config['text']
self.Font = config['font']
etc...
Run Code Online (Sandbox Code Playgroud)
很简单:
for key, value in config.iteritems():
# Customize key here, for example transform 'text_height' to 'TextHeight' or
# use 'key.title()' to obtain titlecased keys...
setattr(self, key, value)
Run Code Online (Sandbox Code Playgroud)
您可能想要这样做key.title(),但在python中建议实例属性全部小写.