如何使用字典键,值对设置类实例属性" pythonic" ly?

时间:2015-11-09 17:57:21

标签: python dictionary class-instance-variables

我创建了一些Python类作为多变量数据结构,然后用于各种任务。在某些情况下,我喜欢使用各种值集填充类。默认参数filename" ho2.defaults"看起来像这样:

name = 'ho2'
mass_option = 'h1o16'
permutation = 'odd'
parity = 'odd'
j_total = 10
lr = 40
br = 60
jmax = 60
mass_lr = 14578.471659
mass_br = 1781.041591
length_lr = ( 1.0, 11.0, 2.65 )
length_br = ( 0.0, 11.0, 2.46 )
use_spline = True
energy_units = 'au'
pes_zpe = -7.407998138300982E-2
pes_cutoff = 0.293994

目前,我通过从文件中读取所需的键,值对来创建字典,现在我想要一个" pythonic"使这些字典键的方法是类实例变量名,即

# Instantiate Molecule Class
molecule = Molecule()

# Create Dictionary of default values
default_dict = read_dict_from_file(filename)

# Set populate class instance variables with dictionary values
for key,value in default_dict:
    molecule.key = value

所以Class的实例变量" molecule.name"可以使用字典键,值对设置。我可以手动完成这项工作,但我确信有更好的方法可以循环使用它。实际上,字典可能很大,我宁愿允许用户选择他们想要填充的值,因此字典可能会改变。我在这里缺少什么?

3 个答案:

答案 0 :(得分:3)

简单的方法是:

vars(molecule).update(default_dict)

这会破坏任何预先存在的属性。对于更精细的方法,尝试:

for name, value in default_dict.items():
    if not hasattr(molecule, name):
        setattr(molecule, name value)

答案 1 :(得分:2)

您可以使用setattrsetattr(molecule, key, value)

答案 2 :(得分:0)

我反转逻辑,以便对象动态回答问题:

class Settings(object):
    ATTRS = {'foo', 'bar'}

    def __init__(self, defaults):
        self.__dict__['data'] = defaults.copy()

    def __getattr__(self, key):
        if key not in self.ATTRS or key not in self.data:
            raise AttributeError("'{}' object has no attribute '{}'".format(
                self.__class__.__name__, key))
        return self.data[key]

    def __setattr__(self, key, value):
        self.data[key] = value


s = Settings({'a': 'b', 'foo': 'foo!', 'spam': 'eggs'})
print s.foo

try:
    print s.spam
except AttributeError:
    pass
else:
    raise AssertionError("That should have failed because 'spam' isn't in Settings.ATTRS")

try:
    print s.bar
except AttributeError:
    pass
else:
    raise AssertionError("That should have failed because 'bar' wasn't passed in")

class Molecule(settings):
    ATTRS = {'name', 'mass_option', ...}

molecule = Molecule(default_dict)