简体   繁体   English

Python上“def”之后的字符串是什么? (不是文档字符串)

[英]What is the string following the “def” on Python? (not the docstring)

I am watching videos of CS262 on Udacity and I am confused by this kind of procedure definitions: 我正在观看关于Udacity的CS262的视频,我对这种程序定义感到困惑:

def t_WORD(token):
    r'[^ <>]+' # any reg. exp. ruleset is placed here
    # ... more processing
    # ... more processing
    return token

This code uses the library ply (.lex) 此代码使用库层(.lex)

I have some Python experience and knowledge but I am quite confused here by the first line coming after the procedure definition line. 我有一些Python的经验和知识,但我对此过程定义行之后的第一行非常困惑。

How is that string (reg. exp. string) is used, interpreted or accessed by the Python interpreter? Python解释器如何使用,解释或访问该字符串(reg.exp。string)? It's just a string not assigned without a variable pointing to it. 它只是一个没有指向它的变量而没有分配的字符串。

I have done the usual Google and SO search but could not find what it actually is. 我已经完成了通常的Google和SO搜索,但无法找到它实际上是什么。

Thank you in advance for all the answers and explanations. 提前感谢您的所有答案和解释。

That string is the docstring, accessible as t_WORD.__doc__ . 该字符串是docstring,可以作为t_WORD.__doc__访问。 The PLY library uses it to assign rules to functions. PLY库使用它为函数分配规则。

In this case, PLY is using docstrings for its own purpose. 在这种情况下,PLY正在使用docstrings来达到自己的目的。 The string is assigned to the __doc__ attribute, and anyone can read the string. 该字符串被分配给__doc__属性,任何人都可以读取该字符串。 In this case, PLY uses it to construct the parser. 在这种情况下,PLY使用它来构造解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM