Indentation problem

Andy Gimblett gimbo at ftech.net
Mon Mar 25 09:03:52 EST 2002


On Mon, Mar 25, 2002 at 02:16:49PM +0100, Gilles Diribarne wrote:

> I've some problem with identing code. I'm allright with the example.
> But, I think it's important to have an "end" building block like "!:"
> 
> What do you think about that?

I think you're wrong to think that it's important to have such a
construct.  The reasons why I think that are:

  * It's clearly not necessary for the language to "work" (otherwise
    it wouldn't).  By that I mean we can write parsers, compilers,
    etc.

  * It's also in fact beneficial for a number of reasons, to do with
    readability, umambiguity, and elimination of religious wars, as
    discussed in an FAQ which I will link to later...

  * I haven't seen any arguments to convince me otherwise (including
    yours, which I'll examine shortly).

> Why I would like to put this in the code?
> 1/ Because it's almost impossible to paste python code with ident = n 
> spaces to another code with indent = m spaces

Is this a problem you've come across much?  Or just a hypothetical
situation?

I don't think it _is_ impossible, it just might be slightly hard work.
If the original programmers paid attention to the style guide, this
wouldn't be a problem of course.  And if you're dealing with enough
code for this to be problematic, I think you need to ask yourself if
copy-and-paste is the right thing to do...

> This kind of examples occurs more and more and you can indent the code 
> by hand when it's 10 lines of codes, but not 1000 code lines.
> It prevents the re-use of code pieces.

Only if copy-and-paste is the only way to re-use code pieces, which it
isn't.  I would hope that 1000 lines of code would be structured into
functions, classes, maybe even modules, in which case you probably
don't need to edit the code at all.  Just import the module and use
what you need.

I'm not saying that you should never copy-and-paste, though.
Sometimes, yes, it's the right thing to do.  In which case...

> 2/ Programmers wants independence: some wants to manage building blocks 
> with spaces, other with tabs, some wants length = 4 with tabs.
> I would like to use my own programming style! But, my code risks to be 
> not supported by others.

There's a python style guide here:

http://python.sourceforge.net/peps/pep-0008.html

If you care about your code being supported by others, I highly
recommend you read it and apply it.  It happens to recommend that you
use 4 spaces, and no tabs whatsoever.  Emacs mode in python takes care
of this very nicely.

You can't say in one breath that you want independence to write code
in any style you like, and then complain in the next breath that other
programmers, whose style differs, don't want to read it (and thus
don't want to use it because they don't understand it or trust it).
This is why it's _good_ that python doesn't use delimiters: we don't
have religious wars over where to put the braces.  OK, so we might
argue over how many spaces to use, but a) you get that argument in
languages with delimiters _too_, and b) Guido says 4, so use 4.  ;-)

> EXAMPLE:
>
> pythona.py:
>  def func():
>    a = 1
>    if a == 1:
>      a ==2
>
> pythonb.py
>    def func2():
>        a = 2
>        if a == 2:
>             print "Yeah!"
>
> You cannot merge correctly or tell me how to do this ?

Easy.  Let's say we want to keep 4 spaces of indentation, as Guido
recommends in the style guide - so we're going to change func() and
keep func2() as it is.  In our favourite editor we mark a region
covering all of func(), and use search-and-replace to turn all
occurrences of "  " (2 spaces) into "   " (4 spaces).  Tah-dah!  If
the code contains any strings which might have multiple spaces in
them, we'll need to be careful, but I think we can handle that.

Of course, this won't work if func() isn't self-consistent about its
indentation, but frankly I don't think func() deserves to run if
that's the case.  I don't know if python will let it or not, because
I've never tried.

By the way, I _think_ python-mode in emacs actually _is_ capable of
changing an entire document's indentation from n-spaces to m-spaces
automatically and intelligently, simply by changing a variable - but I
might be wrong.

> 3/ A large number of programming language use this. C, PERL, Ruby, ...
> Why not Python?

It's called "progress".

http://www.python.org/cgi-bin/faqw.py?req=show&file=faq06.002.htp

-Andy

-- 
Andy Gimblett - Programmer - Frontier Internet Services Limited
Tel: 029 20 820 044 Fax: 029 20 820 035 http://www.frontier.net.uk/
Statements made are at all times subject to Frontier's Terms and
Conditions of Business, which are available upon request.




More information about the Python-list mailing list