Python end of file marker similar to perl's __END__

Stargaming stargaming at gmail.com
Wed Aug 1 07:59:32 EDT 2007


On Wed, 01 Aug 2007 06:56:36 -0400, Steve Holden wrote:

> Stargaming wrote:
>> On Wed, 01 Aug 2007 05:44:21 +0000, Michele Simionato wrote:
>> 
>>> On Aug 1, 5:53 am, beginner <zyzhu2... at gmail.com> wrote:
>>>> Hi All,
>>>>
>>>> This is just a very simple question about a python trick.
>>>>
>>>> In perl, I can write __END__ in a file and the perl interpreter will
>>>> ignore everything below that line. This is very handy when testing my
>>>> program. Does python have something similar?
>>> I wished from something like that. What you can do at the moment, is
>>> to comment or triple quote the code you don't want to run.
>> 
>> Or, if in a function body, you could insert a `return` statement. When
>> in top-level code, invoking `sys.exit` (or exit/quit) can do the trick.
>> A ``raise Exception`` might help, too, but could be kinda distracting
>> sometimes.
>> 
>> So, there is no general purpose solution as perl has it (I guess that
>> __END__ works everywhere at least), rather different solutions for
>> different cases.
> 
> I think you have missed the point. A return statement or call to
> sys.exit() doesn't remove the requirement that the rest ofthe source
> file be legal Python. In a Perl program you can put anything you like
> after __END__.
> 
> regards
>   Steve

That was my point actually. No, there is no such general purpose solution 
as in perl, but if he just wanted to quit execution (to, eg., not commit 
changes to his database), this would be the way to go. Multiline strings 
are the other way to include (nearly) arbitrary data.



More information about the Python-list mailing list