这是如何有效的Python语法?

2024-10-02 12:28:42 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是SqlAlchemy的一部分,我试图找出这是如何有效的Python语法。我知道代码是有效的,我只是想了解这里在做什么,以及它是如何有效的。这些不是嵌套类,而是独立类。我本以为在每个类声明之后都会有一个pass,但我很确定它的不存在是有意义的。你知道吗

class InstrumentedList(list):
    """An instrumented version of the built-in list."""


class InstrumentedSet(set):
    """An instrumented version of the built-in set."""


class InstrumentedDict(dict):
    """An instrumented version of the built-in dict."""


__canned_instrumentation = {
    list: InstrumentedList,
    set: InstrumentedSet,
    dict: InstrumentedDict,
}

__interfaces = {
    list: (
        {'appender': 'append', 'remover': 'remove',
         'iterator': '__iter__'}, _list_decorators()
    ),

    set: ({'appender': 'add',
           'remover': 'remove',
           'iterator': '__iter__'}, _set_decorators()
          ),

    # decorators are required for dicts and object collections.
    dict: ({'iterator': 'values'}, _dict_decorators()) if util.py3k
    else ({'iterator': 'itervalues'}, _dict_decorators()),
}

Tags: oftheindecoratorsanversiondictlist
1条回答
网友
1楼 · 发布于 2024-10-02 12:28:42

只要类定义中有某物,它就是有效的python语法。你知道吗

例如:

class FooBar:
    """ foobar """

这没关系,因为类定义中有一些东西(在本例中是docstring,可以通过__doc__属性访问)。你知道吗

但是,类定义中没有任何内容是无效语法:

class FooBar:

运行此操作时,将生成输出:

SyntaxError: unexpected EOF while parsing

相关问题 更多 >

    热门问题