Python end of file marker similar to perl's __END__

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Thu Aug 2 08:40:50 EDT 2007


On Thu, 02 Aug 2007 10:00:04 +1000, Ben Finney wrote:

> beginner <zyzhu2000 at gmail.com> writes:
> 
>> Thanks everyone for responding. It doesn't look like python has
>> it. I would definitely miss it. As Steve said, the nice thing about
>> __END__ is that things below __END__ do not have to have legit
>> syntax.
> 
> I think the unaddressed question is: Why is there so much code in your
> module with invalid syntax that this trick would be useful?

It's not just "bad syntax" that makes the triple-quote or comment trick
useful. Sometimes you're experimenting, or perhaps tinkering is a better
description. Your aim isn't to end up with a working piece of code, but to
learn something (e.g. "how do decorators work?"). You may end up with
working code at the end, but the finished code isn't the aim of the
exercise and may not be kept.

Because you're experimenting, you might end up with ten different versions
of a function, and not all of them will compile, let alone execute
correctly. It's handy to be able to comment them out and reload() the
file, and while some editors will do bulk comment/uncomment of text, the
triple-quoted string trick is easy enough that you can use it in
Microsoft's Notepad.


>> That let me focus on the lines of code I am debugging and do not
>> have to worry about some bad syntax down the line. This feature is
>> especially handy if I am, saying, replacing modoules or changing
>> data structures.
> 
> I would strongly recommend you examine what part of your practice is
> leading you to write so much code with invalid syntax, without
> immediately testing and fixing it. Eliminate that part of the process
> -- possibly with test-driven development.

While the discipline of TDD is good and useful, there's a time and place
for unstructured and informal experimentation too.


-- 
Steven.




More information about the Python-list mailing list