Python:在未绑定的方法调用中没有参数'filenames'的值

Tha*_*dan 2 python

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

c = Config(test.cfg)
Run Code Online (Sandbox Code Playgroud)

我收到错误:

"TypeError: read() missing 1 required positional argument: 'filenames'
"
Run Code Online (Sandbox Code Playgroud)

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

"No value for argument 'filenames' in unbound method call"
Run Code Online (Sandbox Code Playgroud)

我认为错误是上面的错误的原因.但我不知道为什么.我认为该方法是调用绑定的,因为我使用self作为参考,但我不是shure而且我不明白为什么它不接受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])   
Run Code Online (Sandbox Code Playgroud)

Tom*_*ter 10

你没有ConfigParser正确地在线上初始化read- 你错过了()实际创建实例.

尝试:

if typ is "main":
    self.cfg = ConfigParser()
    self.cfg.read(cfg)
Run Code Online (Sandbox Code Playgroud)


Dan*_*man 5

ConfigParser将其分配给self.scfgsself.cfg属性时,您无需实例化;因此,当您对其进行调用read时,您是通过该类而不是实例上的方法来调用一个未绑定的方法。

它应该是:

self.cfg = ConfigParser()
Run Code Online (Sandbox Code Playgroud)

等等