获取 `object.__doc__` 作为原始字符串

发布于 2024-08-12 14:46:04 字数 111 浏览 2 评论 0原文

除了在源代码中的文档本身前面添加“r”之外,是否有办法将 object.__doc__ 作为原始字符串获取?

我里面有乳胶代码,'\r's、'\f's 等会产生问题。

Is there a way to get object.__doc__ as a raw string, apart from adding an 'r' in-front of the doctring itself in the source code?

I have latex code inside and the '\r's, '\f's etc are creating problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

演出会有结束 2024-08-19 14:46:04

不存在“原始字符串”这样的 Python 类型——存在原始字符串文字,它们只是一种语法方法(众多方法中的一种)指定字符串类型的常量(即文字)。因此,“获取”某些“作为原始字符串”是没有意义的。您可以编写文档字符串作为原始字符串文字(即,使用前缀r - 这正是表示原始字符串文字的方式,这是将此类常量标识为Python 编译器),或者将其中的任何反斜杠加倍(指定包括反斜杠字符的常量字符串的另一种方法),但这与以某种方式“获取”它们无关。

There's no such Python type as "raw string" -- there are raw string literals, which are just one syntax approach (out of many) to specify constants (i.e., literals) that are of string types. So "getting" something "as a raw string" just makes no sense. You can write docstrings as raw string literals (i.e., with the prefix r -- that's exactly what denotes a raw string literal, the specific syntax that identifies such a constant to the Python compiler), or else double up any backslashes in them (an alternative way to specify constant strings including backslash characters), but that has nothing to do with "getting" them one way or another.

弱骨蛰伏 2024-08-19 14:46:04

原始字符串和其他字符串之间的区别只是源代码文字语法的问题。一旦解析,就不存在“原始”字符串对象。 repr(object.__doc__) 的结果始终是这样的,您可以将结果复制并粘贴到 python 源脚本中并获取原始字符串。

考虑:

>>> def foo():
...     'foo\nbar'
...     pass
...
>>> foo.__doc__
'foo\nbar'
>>> print foo.__doc__
foo
bar
>>>

the difference between a raw string and otherwise is just a matter of source code literal syntax. once parsed, there is no 'raw' string object. the result of repr(object.__doc__) will always be such that you can copy and paste the result into a python source script and get the original string.

consider:

>>> def foo():
...     'foo\nbar'
...     pass
...
>>> foo.__doc__
'foo\nbar'
>>> print foo.__doc__
foo
bar
>>>
绿光 2024-08-19 14:46:04

不,你必须添加 r。如果不添加 r,则无论您做什么都无法确保返回原始字符串。

如果您不喜欢原始字符串,另一种选择是使用额外的反斜杠转义字符串中的反斜杠。

No, you have to add the r. If you don't add the r, there's no way you can be sure to get back the original string no matter what you do.

If you don't like raw strings the other alternative is to escape your backslashes in the strings with an extra backslash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文