4

我创建了一些 Python 类用作多变量数据结构,然后用于各种任务。在某些情况下,我喜欢用各种值集填充类。默认参数文件名“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

所以类的实例变量“molecule.name”可以用字典键值对设置。我可以手动完成,但我确信有更好的方法来循环它。实际上,字典可能很大,我宁愿让用户选择他们想要填充的值,以便字典可以更改。我在这里想念什么?

4

3 回答 3

4

您将使用setattrsetattr(molecule, key, value)

于 2015-11-09T18:00:17.857 回答
4

简单的方法是:

vars(molecule).update(default_dict)

不过,这将破坏任何预先存在的属性。对于更微妙的方法,请尝试:

for name, value in default_dict.items():
    if not hasattr(molecule, name):
        setattr(molecule, name value)
于 2015-11-09T18:02:15.360 回答
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)
于 2015-11-09T18:18:48.807 回答