简体   繁体   English

在Python中,模拟Perl的__END__的最佳方法是什么?

[英]In Python what's the best way to emulate Perl's __END__?

Am I correct in thinking that that Python doesn't have a direct equivalent for Perl's __END__ ? 我是否认为Python与Perl的__END__没有直接对应关系是__END__吗?

print "Perl...\n";

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

One thought that occurred to me was to use a triple-quoted string. 我想到的一个想法是使用三引号字符串。 Is there a better way to achieve this in Python? 有没有更好的方法可以在Python中实现呢?

print "Python..."

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

The

__END__
block in perl dates from a time when programmers had to work with data from the outside world and liked to keep examples of it in the program itself. perl的阻塞时间可以追溯到程序员不得不处理来自外部世界的数据并且喜欢将其示例保存在程序本身中的时间。

Hard to imagine I know. 很难想象我知道。

It was useful for example if you had a moving target like a hardware log file with mutating messages due to firmware updates where you wanted to compare old and new versions of the line or keep notes not strictly related to the programs operations ("Code seems slow on day x of month every month") or as mentioned above a reference set of data to run the program against. 例如,如果由于固件更新而使移动目标(例如带有硬件消息的带有可变消息的变异的硬件日志文件)变得有用,那么您想比较该行的新旧版本或使注释不严格与程序操作相关(“代码看起来很慢或每个月的第x天”)或如上所述的运行程序所依据的参考数据集。 Telcos are an example of an industry where this was a frequent requirement. 电信公司是一个经常需要该行业的例子。

Lastly Python's cult like restrictiveness seems to have a real and tiresome effect on the mindset of its advocates, if your only response to a question is "Why would you want to that when you could do X?" 最后,如果您对一个问题的唯一回答是“为什么可以做X时为什么要这么做?”,Python的限制性之类的崇拜似乎对其倡导者的思维方式产生了真正而令人讨厌的影响。 when X is not as useful please keep quiet++. 当X不太有用时,请保持安静++。

The triple-quote form you suggested will still create a python string, whereas Perl's parser simply ignores anything after __END__ . 您建议的三引号形式仍然会创建一个python字符串,而Perl的解析器只是忽略__END__之后的任何__END__ You can't write: 你不能写:

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

Comments are more suitable in my opinion. 我认为评论更合适。

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

What you're asking for does not exist. 您要的内容不存在。 Proof: http://www.mail-archive.com/python-list@python.org/msg156396.html 证明: http : //www.mail-archive.com/python-list@python.org/msg156396.html

A simple solution is to escape any " as \\" and do a normal multi line string -- see official docs: http://docs.python.org/tutorial/introduction.html#strings 一个简单的解决方案是转义任何“ \\”并执行普通的多行字符串-参见官方文档: http : //docs.python.org/tutorial/introduction.html#strings

( Also, atexit doesn't work: http://www.mail-archive.com/python-list@python.org/msg156364.html ) (同样,atexit不起作用: http : //www.mail-archive.com/python-list@python.org/msg156364.html

Hm, what about sys.exit(0) ? 嗯, sys.exit(0)怎么样? (assuming you do import sys above it, of course) (当然,假设您确实在上面import sys

As to why it would useful, sometimes I sit down to do a substantial rewrite of something and want to mark my "good up to this point" place. 至于为什么有用,有时我会坐下来重写一些东西,并想标记我的“到目前为止”。

By using sys.exit(0) in a temporary manner, I know nothing below that point will get executed, therefore if there's a problem (eg, server error) I know it had to be above that point. 通过临时使用sys.exit(0) ,我知道低于该点的任何内容都不会执行,因此,如果有问题(例如服务器错误),我知道它必须高于该点。

I like it slightly better than commenting out the rest of the file, just because there are more chances to make a mistake and uncomment something (stray key press at beginning of line), and also because it seems better to insert 1 line (which will later be removed), than to modify X-many lines which will then have to be un-modified later. 我喜欢它比注释掉文件的其余部分稍微好一点,这是因为有更多的机会犯错并取消注释(在行首按组合键),并且因为插入1行似乎更好(这会稍后删除),然后修改X-many线,以后必须不进行修改。

But yeah, this is splitting hairs; 但是,是的,这简直是胡扯。 commenting works great too... assuming your editor supports easily commenting out a region, of course; 注释也很好用...当然,假设您的编辑器支持轻松注释掉某个区域; if not, sys.exit(0) all the way! 如果没有,则sys.exit(0)一直!

Python does not have a direct equivalent to this. Python没有与此直接等效的方法。

Why do you want it? 你为什么需要它? It doesn't sound like a really great thing to have when there are more consistent ways like putting the text at the end as comments (that's how we include arbitrary text in Python source files. Triple quoted strings are for making multi-line strings, not for non-code-related text.) 当有更多一致的方式(例如,将文本放在末尾作为注释时,这听起来不是一件好事)(这就是我们在Python源文件中包括任意文本的方式。三引号字符串用于制作多行字符串,不适用于与代码无关的文本。)

Your editor should be able to make using many lines of comments easy for you. 您的编辑器应该能够使您轻松使用多行注释。

I use __END__ all the time for multiples of the reasons given. 出于多种原因,我__END__都使用__END__ I've been doing it for so long now that I put it (usually preceded by an exit('0'); ), along with BEGIN {} / END{} routines, in by force-of-habit. 我已经做了很久了,以至于我习惯了将它(通常在exit('0'); )和BEGIN {} / END{}例程一起使用。 It is a shame that Python doesn't have an equivalent, but I just comment-out the lines at the bottom: extraneous, but that's about what you get with one way to rule them all languages. 很遗憾,Python没有等效语言,但是我只在底部注释掉了以下几行:无关紧要的,但这就是用一种方法统治所有语言的结果。

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

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