Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中获取@classmethod的内部状态_Python_Syntax_Scope_Scoping - Fatal编程技术网

在Python中获取@classmethod的内部状态

在Python中获取@classmethod的内部状态,python,syntax,scope,scoping,Python,Syntax,Scope,Scoping,我正在使用一个库,其中定义了以下函数: @classmethod def from_file(cls, path): try: with open(path) as config_file: content = yaml.safe_load(config_file) except IOError: log.error(u"Can't open configuration file '%s'" % path)

我正在使用一个库,其中定义了以下函数:

@classmethod
def from_file(cls, path):

    try:
        with open(path) as config_file:
            content = yaml.safe_load(config_file)
    except IOError:
        log.error(u"Can't open configuration file '%s'" % path)
        raise ConfigurationError

    return cls(content)
我正在以以下方式从我自己的类调用它:

class MyConfig(object):

    def __init__(self, custom_base_path=None, mappings_file_name=None):

    ....
            self.__cfg = self.__load_mappings_file(file=mappings_file_name)

    ....
AttrHolder类在同一模块中定义:

class AttrHolder(Config):
            schema = {
                "subject": {
                    "email": StringEntry,
                    "name": StringEntry
                    },
                "entitlements": {
                    "containerName": StringEntry(required=True)
                    },
            }
下面是第三方配置的初始化代码:

我想知道如何在我的类配置中使用第一节中的内容字段?我之所以需要这样做,是因为我通过调试发现AttrHolder.from_文件调用返回传递给它的结构的残缺表示。我想保留内容字段中的原始表示形式


谢谢。

只需使用您自己的AttrHolder子类即可:


然后从_文件中,cls将成为MyAttrHolder。这就是classmethod的要点。

第一个代码段中的from_filecls,path方法属于AttrHolder类还是来自其他类?如果是的话,AttrHolder类的init方法是什么样子的?不,它是一个不同于导入库的方法。AttrHolder是在MyConfig类中定义的。我粘贴了从另一个库中的Config类继承的AttrHolder的完整内容。我也会为它粘贴代码。是的,正确。我粘贴了Config类的uu init。谢谢
class AttrHolder(Config):
            schema = {
                "subject": {
                    "email": StringEntry,
                    "name": StringEntry
                    },
                "entitlements": {
                    "containerName": StringEntry(required=True)
                    },
            }
def __init__(self, content, _schema=None):
    if _schema:
        self.schema = _schema

    if self.schema and (not content or not isinstance(content, dict)):
        log.error(u"Invalid configuration")
        raise ConfigurationError

    self.entries = {}
    self.children = set()

    for key, value in self.schema.items():
        if isinstance(value, dict):
            setattr(self, key, Config(content.get(key), _schema=value))
            self.children.add(key)
        elif isinstance(value, ConfigEntry):
            self.parse_entry(key, value, (content or {}).get(key))
        else:
            log.warning(u"Invalid entry in configuration content: %s" % key)
class MyAttrHolder(AttrHolder):
    def __init__(self, content, _schema=None):
        do_things_with(content)
        super(MyAttrHolder, self).__init__(content, _schema)

...

MyAttrHolder.from_file(filename)