Skip to main content
 首页 » 编程设计

python之修改后将 *args, **kwargs 传递给父类

2024年10月24日10wayfarer

基于 previous post ,我实现了以下修改后的 argparse.Action,它生成键值对。

我想允许动态设置 nargs,并提供使用 defaultdict(list) 的选项,以应对键可能出现多次的情况。

这是我的实现:

import argparse 
from collections import defaultdict 
 
class NamespaceAction(argparse.Action): 
    """ 
    This modified action allows me to group together key-value pairs passed to an argument. 
    Extended to produce a defaultdict(list) if requested, so that a key can appear more than once. 
    """ 
    def __init__(self, *args, **kwargs): 
        self.nargs = kwargs.get('nargs', 1) 
        self.mode = kwargs.get('mode', 'dict')  # default mode is dict 
        super(NamespaceAction, self).__init__(*args, **kwargs) 
 
    def __call__(self, parser, namespace, values, option_string=None): 
        # The default value is often set to None rather than an empty list. 
        current_arg_vals = getattr(namespace, self.dest, []) or [] 
        setattr(namespace, self.dest, current_arg_vals) 
        arg_vals = getattr(namespace, self.dest) 
        try: 
            if self.mode == 'dict': 
                arg_vals.append(argparse.Namespace(**dict(v.split('=') for v in values))) 
            elif self.mode == 'defaultdict': 
                d = defaultdict(list) 
                for v in values: 
                    v = v.split('=') 
                    d[v[0]].append(v[1]) 
                arg_vals.append(argparse.Namespace(**d)) 
            else: 
                raise NotImplementedError("only dict or defaultdict") 
        except TypeError: 
            raise RuntimeError('Group {} appears to be incorrectly formatted'.format(values)) 

但是,使用它会导致 TypeError: __init__() got an unexpected keyword argument 'mode'。但是,如果我从 args/kwargs 中删除 mode 和 dict,它仍然不起作用:

def __init__(self, *args, **kwargs): 
    self.nargs = kwargs.get('nargs', 1) 
    self.mode = kwargs.get('mode', 'dict')  # default mode is dict 
    if 'mode' in kwargs: 
        del kwargs['mode'] 
    if 'nargs' in kwargs: 
        del kwargs['nargs'] 
    super(NamespaceAction, self).__init__(*args, **kwargs) 

导致 TypeError: __init__() takes at least 3 arguments (1 given)。

我怎样才能让这个类接受参数,并且仍然正确地初始化父类?

请您参考如下方法:

您期望 nargs 是位置性的,但它是 argparse.Action 的可选参数;根据文档:

The Action class must accept the two positional arguments plus any keyword arguments passed to ArgumentParser.add_argument() except for the action itself.

你可以试试:

def __init__(self, *args, **kwargs): 
    # To force nargs, look it up, but don't bother setting on self; 
    # just let it pass to the parent, if you remove it from kwargs, the 
    # parent __init__ overwrites self.nargs with None 
    nargs = kwargs['nargs']  
    assert isinstance(nargs, int) or nargs == "+" 
    # dict.pop is like get, but removes the mapping if it exists 
    self.mode = kwargs.pop('mode', 'dict')  # Will use 'dict' as default 
 
    # Pass along remaining arguments 
    super(NamespaceAction, self).__init__(*args, **kwargs)