简体   繁体   中英

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__ ?

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.
"""

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.

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++.

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 /")

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

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/python-list@python.org/msg156364.html )

Hm, what about sys.exit(0) ? (assuming you do import 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 (eg, 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!

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.

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 an exit('0'); ), along with BEGIN {} / 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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