[Python-ideas] Indentation, again: Problems with moving and combining code across editors.

Andrew Barnert abarnert at yahoo.com
Fri Aug 29 23:04:24 CEST 2014


On Aug 29, 2014, at 12:12, Milind Khadilkar <zedobject at gmail.com> wrote:

> Hi!
> I do appreciate that indentation in Python code shows intention, and I am all for it, but problems do arise if code is copied from one place to another, even from specialized Python editors.... and there is little you can do to recover intentions/indentations lost in the process.

I copy and paste code between editors all the time--e.g., from emacs to the graphical editors on sites like Github or StackOverflow--and I can't remember the last time I've had this problem, except when copying someone else's code that mixed spaces and tabs (and even in that case, it more often helps me find existing invisible errors than it causes errors).

> Besides, spaces become difficult to count and distinguish from tabs, adding to the problem. (Why allow tabs at all?)

That one I could get behind, but it's been suggested and rejected enough times to have a PEP assigned to it.

> Why can't Python be made to accept the following indentation of code (in addition to the current schemes)? There could be some flexibility of choosing the indentation character. (Pipe was mentioned as an alternative in another thread)
> 
> for i in range(10):
> .if i>5:
> ..print 10-i
> .else:
> ..print i
> print "!"

The first problem is trying to come up with a syntax that isn't ambiguous to the parser or to a human. You haven't succeeded there:

    .2

Is that the int literal `2` indented, or the float literal `.2` in the left column?

Even the pipe character had this problem:

    |2

That pipe could mean the or operator if the previous line ended with a continuation character. But even discounting that, the tokenizer and any human experienced with Python will read it as the or operator and then have to follow some rule saying "an or operator at the start of a line that isn't part of a valid expression counts as a space". And that means guessing that it wasn't intended as an or operator, possibly turning what should have been identified as a simple SyntaxError into something baffling.

I suppose you could suggest bringing back ` for this purpose, but I doubt anyone will like that.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140829/01a7c1e8/attachment-0001.html>


More information about the Python-ideas mailing list