在 Python 中,模拟 Perl 的 __END__ 的最佳方法是什么? [英] In Python what's the best way to emulate Perl's __END__?

查看:48
本文介绍了在 Python 中,模拟 Perl 的 __END__ 的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为 Python 没有与 Perl 的 __END__ 直接等效的说法是否正确?

Am I correct in thinking that that Python doesn't have a direct equivalent for Perl's __END__?

print "Perl...\n";

__END__
End of code. I can put anything I want here.

我想到的一个想法是使用三引号字符串.在 Python 中是否有更好的方法来实现这一点?

One thought that occurred to me was to use a triple-quoted string. Is there a better way to achieve this in Python?

print "Python..."

"""
End of code. I can put anything I want here.
"""

推荐答案

您建议的三重引号形式仍将创建一个 python 字符串,而 Perl 的解析器只是忽略 __END__ 之后的任何内容.你不能写:

The triple-quote form you suggested will still create a python string, whereas Perl's parser simply ignores anything after __END__. You can't write:

"""
I can put anything in here...
Anything!
"""
import os
os.system("rm -rf /")

我认为评论更合适.

#__END__
#Whatever I write here will be ignored
#Woohoo !

这篇关于在 Python 中,模拟 Perl 的 __END__ 的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆