三重引用字符串作为注释 [英] triple quoted strings as comments
问题描述
我最近在其他地方抱怨Python没有多行
评论。我被告知使用三重引用字符串来制作多行
评论。我的问题是,由于三引号字符串实际上是
a语言构造,它是否会导致运行时构造一个
字符串然后被丢弃,或者是运行时智能足以看到
它没用过,所以它没有构建它?
例子
def fun(self):
""" doc comment
评论专栏2
"""
x = 1
y = 2
"""这个三重引号字符串用作评论
导致在运行时发生的事情超出
只是跳过它?如为字符串分配内存
或者更糟的是垃圾收集?或不是?
""
z = x + y
....
dave howard
I recently complained elsewhere that Python doesn''t have multiline
comments. i was told to use triple quoted strings to make multiline
comments. My question is that since a triple quoted string is actually
a language construct, does it use cause a runtime construction of a
string which is then discarded, or is the runtime smart enough to see
that it isn''t used and so it doesn''t construct it?
example
def fun(self):
"""doc comment
comment line 2
"""
x = 1
y = 2
"""does this triple quoted string used as a comment
cause something to happen at runtime beyond
just skipping over it? Such as allocation of memory for a string
or worse yet garbage collection? or not?
"""
z = x + y
....
dave howard
推荐答案
据我所知,三引号字符串会导致运行时构造,
并且不会被丢弃,这是python语言的好处。
这里是......很有用。
_http://diveintopython.org/power_of_introspection/index.html
最好的方面
as i know, the triple quoted string does cause a runtime construction,
and will not be discarded, and it''s a benefit of python language.
here is sth. useful.
_http://diveintopython.org/power_of_introspection/index.html
best regard
< br>
dmh2000写道:
dmh2000 wrote:
我最近在其他地方抱怨Python没有多行评论。我被告知要使用三重引用字符串来制作多行评论。我的问题是,由于三引号字符串实际上是一种语言结构,它是否会导致运行时构造一个
字符串然后被丢弃,或者运行时是否足够聪明才能看到
它没有被使用,所以它没有构建它?
I recently complained elsewhere that Python doesn''t have multiline
comments. i was told to use triple quoted strings to make multiline
comments. My question is that since a triple quoted string is actually
a language construct, does it use cause a runtime construction of a
string which is then discarded, or is the runtime smart enough to see
that it isn''t used and so it doesn''t construct it?
很容易找到它。把它放在test.py:
''第一个模块评论''
''另一个''
def func():
''第一个func评论''
''另一个''
打印''hi''
''最后一个''
''和最后一个模块评论''
现在做导入测试从Python提示符,然后退出解释器
并执行strings test.pyc在编译的字节码上。
(简短的答案只是第一个这样的评论。)
-Peter
Easy enough to find out. Put this in test.py:
''first module comment''
''another''
def func():
''first func comment''
''another''
print ''hi''
''last one''
''and last module comment''
Now do "import test" from the Python prompt, then exit the interpreter
and do "strings test.pyc" on the compiled bytecode.
(The short answer is only the first such comments are kept.)
-Peter
dmh2000写道:
dmh2000 wrote:
我最近在其他地方抱怨Python没有多行评论。我被告知要使用三重引用字符串来制作多行评论。我的问题是,由于三引号字符串实际上是一种语言结构,它是否会导致运行时构造一个
字符串然后被丢弃,或者运行时是否足够聪明才能看到
它没有被使用,所以它没有构建它?
例子
def fun(self):
""" doc comment
评论第2行
""
x = 1
y = 2
""" ;这个三重引用的字符串是否用作评论
会导致某些事情在运行时发生,而不仅仅是跳过它?如为字符串分配内存还是更糟糕但是垃圾收集?是不是?
"""
z = x + y
I recently complained elsewhere that Python doesn''t have multiline
comments. i was told to use triple quoted strings to make multiline
comments. My question is that since a triple quoted string is actually
a language construct, does it use cause a runtime construction of a
string which is then discarded, or is the runtime smart enough to see
that it isn''t used and so it doesn''t construct it?
example
def fun(self):
"""doc comment
comment line 2
"""
x = 1
y = 2
"""does this triple quoted string used as a comment
cause something to happen at runtime beyond
just skipping over it? Such as allocation of memory for a string
or worse yet garbage collection? or not?
"""
z = x + y
它似乎丢弃了第二个三重引用的评论(第一个是
作为文档字符串保留。)
我创建了两个脚本,一个带有第二个三重引用字符串,
其他没有。编译版本*几乎*相同(一个字节
差异,如果我没有记错的话,来自.pyc文件中嵌入的不同的
文件名)。
30/01/2006 09:34 PM 327 triple.py
30/01/2006 09:35 PM 359 triple.pyc
30/01/2006 09:34 PM 96 triple2.py
30/01/2006 09:35 PM 358 triple2.pyc
$ b $bAndré>
这篇关于三重引用字符串作为注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!