我有一个类来包装我程序的配置内容。 但是当我尝试像这样在 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/