【问题标题】:Constructing Proper Python Facade Class with Super?用 Super 构建正确的 Python 门面类?
【发布时间】:2016-09-29 23:12:42
【问题描述】:

我以为我使用 Super() 处理了多重继承,并试图在 Facade 类中使用它,但我遇到了一个奇怪的错误。我正在使用一款名为 Fireworks 的制作精良的 Python Workflow 软件,但类和 Workflow Tasks 的结构非常严格,因此我创建了一个 Facade 类以方便使用。

以下是工作流任务的基本结构:

class BgwInputTask(FireTaskBase):

    required_params = ['structure', 'pseudo_dir', 'input_set_params', 'out_file']
    optional_params = ['kpoints', 'qshift', 'mat_type']

    def __init__(self, params): 
        self.structure = Structure.from_dict(params.get('structure').as_dict())
        self.pseudo_dir = params.get('pseudo_dir')
        self.kpoints = params.get('kpoints', None)
        self.qshift = params.get('qshift', None)
        self.isp = params.get('input_set_params')
        self.run_type = params.get('run_type', None)
        self.mat_type = params.get('mat_type', 'metal')
        self.filename = params.get('out_file')

        '''
        misc code for storing pseudo_potentials in:
        self.pseudo_files
        self.occupied_bands
        etc...
        '''

        params = {'structure': self.structure, 'pseudo_dir': self.pseudo_dir,
            'kpoints': self.kpoints, 'qshift': self.qshift,
            'input_set_params': self.isp, 'run_type': self.run_type,
            'mat_type':self.mat_type, 'out_file': self.filename}
        self.update(params)

    def write_input_file(self, filename):
        <code for setting up input file format and writing to filename>

我在下面使用super 构建了我的 Facade 类:

class BgwInput(BgwInputTask):

    def __init__(self, structure, pseudo_dir, isp={},
                kpoints=None, qshift=None, mat_type='semiconductor',
                out_file=None):

        self.__dict__['isp'] = isp
        self.__dict__['run_type'] = out_file.split('.')[0]
        self.__dict__['params'] = {'structure': structure, 'pseudo_dir': pseudo_dir,
            'kpoints': kpoints, 'qshift': qshift,
            'input_set_params': self.isp, 'mat_type': mat_type,
            'out_file': out_file, 'run_type': self.run_type}
        print("__init__: isp: {}".format(self.isp))
        print("__init__: runtype: {}".format(self.run_type))

        super(BgwInput, self).__init__(self.params)

    def __setattr__(self, key, val):
        self.proc_key_val(key.strip(), val.strip()) if isinstance(
            val, six.string_types) else self.proc_key_val(key.strip(), val)

    def proc_key_val(self, key, val):
        <misc code for error checking of parameters being set>
        super(BgwInput, self).__dict__['params']['input_set_params'].update({key:val})

这很好用,除了一个让我困惑的小警告。创建BgwInput 的新实例时,它不会创建一个空实例。以前实例中设置的输入集参数以某种方式被传递到新实例,但不是kpointsqshift。例如:

>>> epsilon_task = BgwInput(structure, pseudo_dir='/path/to/pseudos', kpoints=[5,5,5], qshift=[0, 0, 0.001], out_file='epsilon.inp')
__init__: isp: {}
__init__: runtype: epsilon

>>> epsilon_task.epsilon_cutoff = 11.0
>>> epsilon_task.number_bands = 29


>>> sigma_task = BgwInput(structure, pseudo_dir='/path/to/pseudos', kpoints=[5,5,5], out_file='sigma.inp')
__init__: isp: {'epsilon_cutoff': 11.0, 'number_bands': 29}
__init__: runtype: sigma

但是,如果我将 Facade 类中的 self.__dict__['isp'] = isp 更改为 self.__dict__['isp'] = isp if isp else {},一切似乎都按预期工作。在先前实例中设置的参数不会转移到新实例中。那么,为什么 Facade 类不默认为 isp={}(假设这是 __ init __ 中的默认值),如果在创建时没有给定输入集参数,它应该是什么?既然默认应该是一个空白字典,它从哪里提取以前的参数?

为了清楚起见,我有一个解决方案可以使 Facade 类按预期发挥作用(通过将 Facade 中的 isp 更改为 self.__dict__['isp'] = isp if isp else {}),但我试图弄清楚为什么会这样是需要的。我相信我缺少 super 或 Python 中的方法解析顺序的基本内容。我很好奇为什么会发生这种情况并试图扩大我的知识库。

下面是门面类的方法解析顺序。

>>> BgwInput.__mro__

(pymatgen.io.bgw.inputs.BgwInput,
pymatgen.io.bgw.inputs.BgwInputTask,
fireworks.core.firework.FireTaskBase,
collections.defaultdict,
dict,
fireworks.utilities.fw_serializers.FWSerializable, 
object)

【问题讨论】:

  • 您的__init__ 中有一个mutable default argument
  • 感谢您提供此信息。我不知道可变的默认参数。这绝对解释了我所看到的行为。所以,这不是我缺乏知识的supermethod resolution order,而是我缺少的核心Python 论点的一部分。

标签: python inheritance super facade method-resolution-order


【解决方案1】:

python 中的可变默认参数不像你期望的那样工作;您的函数定义(省略不相关的参数):

def __init__(self, ..., isp={}, ...):
   <CODE>

相当于这个:

DEFAULT_ISP = {}
def __init__(self, ..., isp=DEFAULT_ISP, ...):
   <CODE>

(除了DEFAULT_ISP 变量不可用。)

上面的代码清楚地表明你的两个任务正在使用相同的isp 字典,这显然是由属性设置器修改的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2019-04-27
    • 2011-09-26
    • 1970-01-01
    相关资源
    最近更新 更多