我认为Python没有Perl的直接等价物,我是否正确__END__?
print "Perl...\n";
__END__
End of code. I can put anything I want here.
Run Code Online (Sandbox Code Playgroud)
我想到的一个想法是使用三引号字符串.有没有更好的方法在Python中实现这一点?
print "Python..."
"""
End of code. I can put anything I want here.
"""
Run Code Online (Sandbox Code Playgroud)
Mic*_*nny 17
该
__END__Run Code Online (Sandbox Code Playgroud) perl中的块可以追溯到程序员必须处理来自外部世界的数据并且喜欢在程序本身中保留它的示例的时间.
很难想象我知道.
例如,如果你有一个像硬件日志文件一样的移动目标,由于固件更新而你想要比较线路的新版本和新版本,或者保持与程序操作不严格相关的注释("代码似乎很慢"在每个月的第x个月"),或者如上所述,用于运行程序的参考数据集.电信公司是一个经常需要这个行业的例子.
最后,如果对问题的唯一回答是"当你可以做X时,为什么要这样做?",那么像限制性一样的Python似乎对其拥护者的心态产生了真实和无聊的影响.当X不那么有用时请保持安静++.
你建议的三重引用形式仍然会创建一个python字符串,而Perl的解析器之后只会忽略任何内容__END__.你不能写:
"""
I can put anything in here...
Anything!
"""
import os
os.system("rm -rf /")
Run Code Online (Sandbox Code Playgroud)
在我看来,评论更合适.
#__END__
#Whatever I write here will be ignored
#Woohoo !
Run Code Online (Sandbox Code Playgroud)
你要求的东西不存在.证明:http://www.mail-archive.com/python-list@python.org/msg156396.html
一个简单的解决方案是逃避任何"as"并执行正常的多行字符串 - 请参阅官方文档:http://docs.python.org/tutorial/introduction.html#strings
(另外,atexit不起作用:http://www.mail-archive.com/python-list@python.org/msg156364.html)