tags:

views:

232

answers:

4

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.
"""
+2  A: 

What you proposed works. On what grounds would something else be better?

jcdyer
It works as long as you escape your quotes.
Tadeusz A. Kadłubowski
It works as long as you escape your triple quotes. Is this a major inconvenience?
jcdyer
+3  A: 

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 )

Simon B.
+4  A: 

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 !
Tadeusz A. Kadłubowski
@Lott: You can put anything after perl's `__END__`, including perl code, more `__END__` tokens, literally anything.. You cannot put odd number of tripple quotes in the python tripple quoted string (unless you remember to escape it). If you put even number of tripple quotes and some python code, it will be executed.
Tadeusz A. Kadłubowski
Far clearer example than the previous version of this answer.
S.Lott
+1  A: 

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.

Mike Graham