在 Python 中,模拟 Perl 的 __END__ 的最佳方法是什么?
我是否正确地认为 Python 没有 Perl 的 __END__ 的直接等效项?
print "Perl...\n";
__END__
End of code. I can put anything I want here.
我想到的一个想法是使用三引号字符串。在Python中有没有更好的方法来实现这一点?
print "Python..."
"""
End of code. I can put anything I want here.
"""
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.
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.
"""
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这
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.
很难想象我知道。
例如,如果您有一个移动目标(例如由于固件更新而包含变异消息的硬件日志文件),您想要比较该行的新旧版本或保留与程序操作不严格相关的注释(“代码似乎很慢”),那么它很有用每个月的第 x 天”)或如上所述,运行程序所针对的参考数据集。电信公司就是一个经常需要这样做的行业的例子。
最后,如果你对问题的唯一回答是“当你可以做 X 的时候,为什么你会想要那样做?”,Python 的崇拜式限制似乎对其拥护者的心态产生了真实而令人厌烦的影响。当 X 没那么有用时,请保持安静++。
The
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.
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. 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?" when X is not as useful please keep quiet++.
您建议的三引号形式仍然会创建一个 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:Comments are more suitable in my opinion.
你要求的东西不存在。
证明: http://www.mail-archive.com/< span class="__cf_email__" data-cfemail="dfafa6abb7b0b1f2b3b6acab9fafa6abb7b0b1f1b0adb8">[email protected]/msg156396.html
一个简单的解决方案是转义任何“as \”并执行正常的多行字符串 - - 请参阅官方文档: http://docs.python.org/tutorial/introduction.html# strings
(此外,atexit 不起作用:http ://www.mail-archive.com/[电子邮件受保护]/msg156364.html )
What you're asking for does not exist.
Proof: http://www.mail-archive.com/[email protected]/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
( Also, atexit doesn't work: http://www.mail-archive.com/[email protected]/msg156364.html )
由于给出的原因的倍数,我一直使用
__END__
。我已经这样做了很长时间了,现在我把它(通常前面有exit('0');
)和BEGIN {}
/END{}
惯例,出于习惯的力量。遗憾的是 Python 没有对应的东西,但我只是注释掉了底部的几行:无关的,但这就是你用一种方式来统治所有语言所得到的。I use
__END__
all the time for multiples of the reasons given. I've been doing it for so long now that I put it (usually preceded by anexit('0');
), along withBEGIN {}
/END{}
routines, in by force-of-habit. 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.嗯,
sys.exit(0)
怎么样? (当然,假设你在它上面做了import sys
)至于为什么它有用,有时我会坐下来对某些东西进行实质性重写,并想标记我的“到目前为止还不错”的地方。
通过临时使用 sys.exit(0) ,我知道低于该点的任何内容都不会被执行,因此,如果出现问题(例如服务器错误),我知道它必须< /strong> 高于该点。
与注释掉文件的其余部分相比,我更喜欢它,只是因为有更多的机会犯错误并取消注释某些内容(在行首按下杂散按键),而且还因为插入 1 行似乎更好(这将稍后被删除),而不是修改 X 多行,然后必须在以后取消修改。
但是,是的,这真是令人吹毛求疵。评论也很有效……当然,假设您的编辑器支持轻松评论某个区域;如果没有,请一直
sys.exit(0)
!Hm, what about
sys.exit(0)
? (assuming you doimport sys
above it, of course)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 (e.g., server error) I know it had to be above that point.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.
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!这是我在 Perl 中使用 __END__ 的一种方法(但不是唯一的方法),现在我正在对 Perl 与 Python3 进行比较分析,特别是重新正则表达式,最重要的是调试正则表达式。我首先编写我正在测试的任何内容的 Perl 版本。我在顶部放置了两个 shebang,第一个启用了警告 (-w),第二个调用调试器 (-wd),然后根据需要在它们之间进行切换。我为 Python3 添加了两个 shebang,第二个使用 Python 的调试器 (-m pdb)。为了运行 Python3 代码,我将 Python3 shebangs 放在顶部,即我希望运行的 Python 风格的最顶部,并使用 vim 的块命令标记行功能,在每个 Perl 行前面放置一个 #。要恢复到 Perl,请打乱 shebang 并使用 vim 中的块命令取消注释 #。效果很好!任何将 Perl 与 Python 进行比较的人都可以尝试一下。
到目前为止,在我的分析中,在 Python3 中我真的很喜欢
print(p.groupindex)
其中 p 是一个编译的正则表达式,用于检查组名称和数字之间的映射,而在 Perl 中我更喜欢__END__
和正则表达式调试模块。另外,还有 mod_perl,但不再有 mod_python,而且可能永远不会再有。我不太喜欢 OOP,更喜欢函数式编程,两者都适合。当然,如果我喜欢 OOP,我会选择 Python3,因为它干净的多重继承模型。恕我直言,在使用 Perl 30 多年之后。老实说,真正精通 Perl 花了很长时间,但 Python 学起来更快,但也许那是因为我真的精通 Perl 和 C。一如既往,你的里程会有所不同......Here is one way (but not the only one) I use
__END__
in Perl, now that I am doing comparative analysis of Perl versus Python3, particularly re regex and most important, debugging regex. I first write the Perl version of whatever I am testing. I place two shebangs at the top, the first with warnings enabled (-w), the second invoking the debugger (-wd), and I switch between them as needed. I added two more shebangs for Python3, the second using Python's debugger (-m pdb). To run Python3 code I place the Python3 shebangs at the top, the topmost whichever flavor of Python I wish to run and using vim's mark line feature for block commands, place a single # in front of each Perl line. To revert to Perl, shuffle the shebangs and uncomment one # using a block command in vim. Works great! Take it for a test drive, anyone comparing Perl against Python.So far, in my analysis, in Python3 I really like
print(p.groupindex)
where p is a compiled regex, to examine the mapping between group names and numbers, while in Perl I prefer__END__
and the regex debugging modules. Also, there is mod_perl but no longer a mod_python, and probably never will be again. I'm not big on OOP, prefer functional programming for which either is suitable. Of course, if I was into OOP, my choice would be Python3 for its clean multiple inheritance model. Just IMHO, after 30+ years with Perl. To be honest, it took a long time to become truly proficient with Perl but Python is quicker to learn, but perhaps that's because I really am proficient in Perl and C. As always, your mileage will vary...好的,所以我是一个 perl 用户,正在研究 Python,并且想要一个解决方案。而且,由于我是一名 Perl 用户,而且现在已经是 2024 年了,所以我不会轻易被吓倒。
所以这里有一个解决方案 - 将文件前面的 #!/usr/local/bin/python3 替换为以下内容:
然后在该文件 (/path/to/somewhere/python-with-end.pl) 中使用这个perl 脚本:
显然,这是一个 hack,但我有一个 t.pl 文件,其中有一些可以追溯到几十年前的 perl 测试片段,其中包含数百个 __END__ ,当我需要时,这非常有用记住如何做一些或尝试一些新功能,从 Python 开始,我甚至更需要做这些小测试片段。所以这个小技巧可以很好地解决这个问题。
OK, so I'm a perl user, looking at Python, and wanting a solution for this. And, since I'm a perl user, and it's 2024, I'm not one to be daunted easily.
So here is a solution - replace the #!/usr/local/bin/python3 at the front of your file with something like:
Then in that file (/path/to/somewhere/python-with-end.pl) use this perl script:
Obviously, this is a bit of a hack, but I have a t.pl file with little perl test snippets dating back decades with hundreds of
__END__
s through it which is great when I need to remember how to do some or try some new facility out, and starting in Python I have even more need to do these little test snippets. So this little hack will do very nicely for resolving this.Python 没有与此直接等效的东西。
你为什么想要它?当有更一致的方法,例如将文本作为注释放在末尾(这就是我们在 Python 源文件中包含任意文本的方式)时,这听起来并不是一件很棒的事情。三重引号字符串用于制作多行字符串,不适用于与代码无关的文本。)
您的编辑器应该能够使您轻松使用多行注释。
Python does not have a direct equivalent to this.
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.)
Your editor should be able to make using many lines of comments easy for you.