这种有效的Python语法是如何实现的?
下面是SqlAlchemy的一部分,我试图弄清楚这是如何有效地使用Python语法的。我知道代码是有效的,我只是想了解这里在做什么,以及它是如何有效的。这些不是嵌套类,而是独立类。虽然我希望在每个类声明之后都能通过,但我很确定没有通过是很重要的这种有效的Python语法是如何实现的?,python,Python,下面是SqlAlchemy的一部分,我试图弄清楚这是如何有效地使用Python语法的。我知道代码是有效的,我只是想了解这里在做什么,以及它是如何有效的。这些不是嵌套类,而是独立类。虽然我希望在每个类声明之后都能通过,但我很确定没有通过是很重要的 class InstrumentedList(list): """An instrumented version of the built-in list.""" class InstrumentedSet(set): """An i
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()),
}
只要类定义中有某些内容,它就是有效的python语法 例如:
class FooBar:
""" foobar """
这没关系,因为在本例中有一个docstring,可以通过类定义中的_udoc_u属性访问它
但是,类定义中没有任何内容是无效语法:
class FooBar:
运行此操作时,将生成以下输出:
SyntaxError: unexpected EOF while parsing
那么,您到底不清楚什么呢?类主体必须包含某些内容,但docstring也算作这些内容。这是一个语句,比如pass。我想我仍然认为像C/C++这样的注释不是代码。我现在对文档字符串有了新的欣赏。