Python:未绑定(bind)方法调用中参数 'filenames' 没有值

标签 python

我有一个类来包装我程序的配置内容。 但是当我尝试像这样在 ipython 中测试 i 时:

c = Config(test.cfg)

我得到错误:

"TypeError: read() missing 1 required positional argument: 'filenames'
"

但是我的 pylint 在我的 emacs 中抛出了这个错误:

"No value for argument 'filenames' in unbound method call"

我认为上面的错误是上面错误的原因。 但我不知道为什么。 我认为该方法是调用绑定(bind)的,因为我使用 self 作为引用,但我不确定,我不明白为什么它不接受 cfg 作为参数。

这是我的课:

from configparser import ConfigParser

class Config:
    """Wrapper around config files
    Args:
        cfg: main config file
    """
    def __init__(self, cfg=None):
        self.cfg = []
        self.cfgraw = cfg
        self.scfgs = []
        self.scfgs_raw = []
        if not cfg is None:
            self.add(typ="main", cfg=cfg)


    def add(self, typ, cfg):
        """add config file
        Args:
             typ: type of file main or sub
        Returns:
             none
        """
        if typ is not "main" and cfg is None:
            raise ValueError('no file provied and type not main')

        if typ is "sub":
            for index in range(len(self.scfgs)):
                self.scfgs[index] = ConfigParser
                self.scfgs[index].read(cfg)

        if typ is "main":
            self.cfg = ConfigParser
            self.cfg.read(cfg)
    def write(self, typ=None, index=None):
        """write changes made
        Args:
            typ: type of target file
            index: if type is sub add index to write selected file
        Returns:
            none
        """
        if typ is "main":
            self.cfg.write(self.cfgraw)
        if typ is "sub":
            if index is None:
                for item in range(len(self.scfgs)):
                    self.scfgs[item].write(self.scfgs_raw[item])
            else:
                self.scfgs[item].write(self.scfgs_raw[item])   

最佳答案

您没有在 read 之前的行中正确初始化您的 ConfigParser - 您缺少 () 来实际创建一个实例.

尝试:

if typ is "main":
    self.cfg = ConfigParser()
    self.cfg.read(cfg)

关于Python:未绑定(bind)方法调用中参数 'filenames' 没有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37749766/

相关文章:

c++ - 在linux中通过C++(或python)访问多个键盘输入

python - matplotlib:隐藏子图并用其他子图填充空间

python - os.walk 还是 glob 更快?

python - 回文发生器

python - Matplotlib 自定义样式默认标题位置

python - 如何向 Tornado 添加自定义事件

python - 使用 Python 构建树

python - 在正值/负值上拆分列?

python - 从控制台 python 同时获取多个输入

python - 比较两个数据帧并获得差异