Python indentation

P

Peter Hansen

Reinhold said:
You mean "all but readable"?

I actually did mean "all but unreadable", and I think that is
an English idiom that I used correctly (in effect, it means
"nearly unreadable").

In any case, I was being too kind: his code was completely
unreadable to the rest of us... :)

-Peter
 
J

John Roth

Sateesh said:
Hi,
I am a beginner in Python, and am wondering what is it about the indentation
in Python, without which python scripts do not work properly.
Why can't the indentation not so strict so as to give better freedom to the
user?
Is there any plausible reason behind this?

Readability. Some people feel it confines them,
they tend to go to other languages.

John Roth
 
R

Reinhold Birkenfeld

Peter said:
I actually did mean "all but unreadable", and I think that is
an English idiom that I used correctly (in effect, it means
"nearly unreadable").

Ok, then it's my lousy English... I though of "alles, außer lesbar".
In any case, I was being too kind: his code was completely
unreadable to the rest of us... :)

*g* I know what you're speaking of...

Reinhold
 
S

Steve Lamb

I am a beginner in Python, and am wondering what is it about the indentation
in Python, without which python scripts do not work properly.
Why can't the indentation not so strict so as to give better freedom to the
user?
Is there any plausible reason behind this?

Could you give an example of what you feel would not be possible in
Python? I have found that when most people hear "significant white space"
they mistakeningly feel that it is like older languages which had the same.
It is not. Thus far I have not found any indention of code that I would want
to do in another language (within reason) which is not possible in Python.
 
I

Istvan Albert

Sateesh said:
Why can't the indentation not so strict so as to give better freedom to the
user?
Is there any plausible reason behind this?

What will bother you even more is that the "official"
recommendation is that you should be using spaces to
indent not tabs... the reason appears to be that tabs
might not copy properly from some terminal windows...

I like python very much, but every time I see a space indented
module the first thing I do is to replace the spaces with
tabs.

I noticed that beginners like to indent more while experts
indent less. Once your eye is trained 2 levels might
be enough. OTOH it depends on the project, with many nested loops
you're better off with wider display. IMHO a display of
3 spaces is the best....

But if you stick with the "official" recommendation
then you have to press four spaces or four deletes on the first level,
eight on the second etc... to align stuff...

Yeah, yeah I know that this or that editor has a shortcut enabled via
CTRL+ALT+INSERT+SPACE+double-click+W and that will do the job in one
keystroke, but sometimes you need to work with unfamiliar editors
and odd circumstances and then the whole spaces vs tabs argument
becomes extremely relevant and the wide adoption of "spaces"
makes it a drag.

one more thing to consider...

I.
 
T

Tor Iver Wilhelmsen

Sateesh said:
Why can't the indentation not so strict so as to give better freedom to the
user?

It's a core syntactical feature of the language. Your request is like
asking, say, a C family language to adopt BEGIN and END instead of
those weird braces, because you're used to it from Pascal.
 
P

Peter Hansen

Istvan said:
What will bother you even more is that the "official"
recommendation is that you should be using spaces to
indent not tabs... the reason appears to be that tabs
might not copy properly from some terminal windows...

Actually, that's only one of several reasons. Another is
that there is no universally agreed-upon definition of how
much indentation is intended for the ASCII TAB character,
which leads to several problems including ambiguity when
spaces and tabs are mixed.

Ambiguity sucks but a SPACE is a SPACE is a SPACE.
I like python very much, but every time I see a space indented
module the first thing I do is to replace the spaces with
tabs.

I had an urge to do that really early on, from some leftover
feeling that I was wasting hard drive space. My advice is
to get over it. You'll sleep better, not worrying about all
the SPACE-infested code out there in the world that you
haven't had a chance to "fix". :)
But if you stick with the "official" recommendation
then you have to press four spaces or four deletes on the first level,
eight on the second etc... to align stuff...

I can't imagine anyone actually thinks this is what they really
are expected to do. If your editor is really broken, use single
spaces in the interim, while you find a non-broken one.
Yeah, yeah I know that this or that editor has a shortcut enabled via
CTRL+ALT+INSERT+SPACE+double-click+W and that will do the job in one
keystroke,

*Any* decent editor can be configured to inject spaces up to the
next defined tab-stop when TAB is hit. By definition, therefore,
any editor that cannot is broken.

-Peter
 
B

bruno modulix

Reinhold Birkenfeld a écrit :
Grant Edwards wrote:
(snip about 'why significative whitespace')
Well, one could apply another coding style in this example:

if (condition) {
doThis();
doThat();
} else {
doWhatever();
andSoOn();
}

which only takes 7 lines and is not much less readable.

For me it's as absolutely unreadable as the first snippet.
The only truely correct layout being (of course !-) :

if (condition)
{
doThis();
doThat();
}
else
{
doWhatever();
andSoOn();
}
But I agree with
you!

I do too. The fact is that there is no brace-and-indent-style-war with
Python !-)

Bruno
 
J

Jarek Zgoda

Peter Hansen said:
*Any* decent editor can be configured to inject spaces up to the
next defined tab-stop when TAB is hit. By definition, therefore,
any editor that cannot is broken.

Isn't "obsolete" an antonime for "decent", rather than "broken"?
 
R

Reinhold Birkenfeld

bruno said:
I do too. The fact is that there is no brace-and-indent-style-war with
Python !-)

As we can see a few threads later, it is replaced by the
tabs-or-spaces-war...

Reinhold
 
B

bruno modulix

Jacek Generowicz a écrit :
Hi,
I am a beginner in Python, and am wondering what is it about the indentation
in Python, without which python scripts do not work properly.
Why can't the indentation not so strict so as to give better freedom to the
user?
Is there any plausible reason behind this?


def ask(lang, noise):
print """I am a beginner in %s, and am wondering what is it about the %s in
%s, without which %s programs do not work properly.
Why can't the %s not so strict so as to give better freedom to
the user?
Is there any plausible reason behind this?""" % (lang, noise, lang, lang, noise)
print

for l,n in [['C', '{,} and ;'],
['C++', '{,} and ;'],
['Java', '{,} and ;'],
['Pascal', 'begin and end'],
['Lisp', '( and )'],
['Perl', '!@#*$&!@$#/\'],
['PHP', '$ and {,} and ;']]:
ask(l,n)

keywords: parody, irony

Keyboard !

(5 minutes later)

Is it GPL'ed ?-)

Bruno
 
B

bruno modulix

Tor Iver Wilhelmsen a écrit :
It's a core syntactical feature of the language. Your request is like
asking, say, a C family language to adopt BEGIN and END instead of
those weird braces, because you're used to it from Pascal.

Quite easy :
#define BEGIN {
#define END }

Bruno
 
S

Steve Lamb

*Any* decent editor can be configured to inject spaces up to the
next defined tab-stop when TAB is hit. By definition, therefore,
any editor that cannot is broken.

What Istvan is using is better known, to me at least, as "the vi
argument". IE, it was best for people to learn vi over another editor because
one could be stuck with nothing but vi to do editing with since all unixes
came with vi stock. Whatever would someone do if they were incapable of using
their preferred editor and stuck *only* with vi!?

Nevermind that there are now several well used flavors of unix which do
not come stock with vi or, at the very least, only vi. And some of the
situations that the people would create. "What if you only had a laptop, an
install disc for intel solaris and were stuck on a stranded isle with no
network access to get anything else!? You really need to conserve battery
power so you can't waste time learning vi then but you need it to write the
perfect program to calculate the best method of communication using firewood.
YOU DON'T KNOW VI!!! WHAT WOULD YOU DO!!!!! AIYEEEEEE!!!!" Ok, I may be
exagerrating a tiny bit but only a tiny bit.

Personally I've not been in a situation where I haven't had a decent
editor to program with in the past decade or two. I use spaces for just the
reason you gave; a SPACE is a SPACE is a SPACE. Anyone who's worried about
how many keystrokes they're going to expend because they're programming Python
on a laptop on a deserted isle in the middle of the pacific with only notepad
really needs to reexamine their priorities.

Oh, and for the record, I can tweak the collective noses of the vi
zealots who came up with such absurd and convoluted reasonings on why everyone
simply MUST learn vi because I use vim. ;)
 
S

Steve Lamb

As we can see a few threads later, it is replaced by the
tabs-or-spaces-war...

Not really. First off if one follows the offical coding style it is 4
spaces. Go against it at your own peril. The braces-placement war isn't
replaced with the tabs-or-spaces war. That presumes the tab-or-spaces war
doesn't exist side-by-side with the braces-placement war. We just notice it
more since we don't have the braces-placement war to detract us. :)
 
C

Christopher Weimann

What Istvan is using is better known, to me at least, as "the vi
argument". IE, it was best for people to learn vi over another editor because
one could be stuck with nothing but vi to do editing with since all unixes
came with vi stock.

Thats not the reason to learn vi, its the reason to learn ed :)
 
D

Dan Bishop

Grant Edwards said:
I am a beginner in Python, and am wondering what is it about
the indentation in Python, without which python scripts do not
work properly. Why can't the indentation not so strict so as
to give better freedom to the user? Is there any plausible
reason behind this?

Yes. It's about readability.

In many languages, such as C or Perl, you can write readable code, but
you can also squeeze your code in as few lines as possible, resulting in
hard to read, hard to maintain, hard to debug code[1]. Whenever I
translate a Perl script into Python, I end up with about 25-40% more
lines, but the script is much more readable than the Perl counterpart
(mostly, of course, because of the lacking $'s ;).

Compare C and Python:

In C, we have 10 lines [compared to 6 in Python]

if (condition)
{
doThis();
doThat();
}
else
{
doWhatever();
andSoOn();
}

Only because you're using an inefficient brace style.

The style I use writes it as:

if (condition) {
doThis();
doThat();
} else {
doWhatever();
andSoOn();
}

which is 7 lines, only 1 more than the Python version.

But this does demonstrate an advantage of Python: No arguments over
where to put braces :)
 
I

Istvan Albert

Peter said:
Ambiguity sucks but a SPACE is a SPACE is a SPACE.

Is there a reason why a TAB is not a TAB is not a TAB?
*Any* decent editor can be configured to inject spaces up to the
next defined tab-stop when TAB is hit. By definition, therefore,
any editor that cannot is broken.

Is it really that trivial to set up an editor so that when one hits
delete/backspace to 'un-align' a line of code it deletes as
many space characters as the soft-tab has inserted?

I don't think so.

Istvan.
 
J

Jarek Zgoda

Istvan Albert said:
Is there a reason why a TAB is not a TAB is not a TAB?

Because TAB is TAB and not 8 * SPACE nor 4 * SPACE nor 2 * SPACE. In
editor you don't see a TAB, you see 8 * SPACE or 4 * SPACE or 2 * SPACE
or any number of spaces you set in your editor options. There's no
display character for TAB, it is substituted by SPACES.

As you see, TAB is down, while spaces are up. Resistance is futile. You
will be assimilated.
 
V

Ville Vainio

Istvan> Is it really that trivial to set up an editor so that when
Istvan> one hits delete/backspace to 'un-align' a line of code it
Istvan> deletes as many space characters as the soft-tab has
Istvan> inserted?

Emacs python-mode does it.

Lesser editors often achieve the same effect by shift-tab.
 
M

Mike C. Fletcher

Jarek said:
Istvan Albert <[email protected]> pisze:




Because TAB is TAB and not 8 * SPACE nor 4 * SPACE nor 2 * SPACE. In
editor you don't see a TAB, you see 8 * SPACE or 4 * SPACE or 2 * SPACE
or any number of spaces you set in your editor options. There's no
display character for TAB, it is substituted by SPACES.

As you see, TAB is down, while spaces are up. Resistance is futile. You
will be assimilated.
Nope. People have been telling me to use spaces in my Python for
*years*, and I'm nowhere near assimilated. The number of silly
occasions where hitting <del> or <backspace> to remove a tab works where
the editor doesn't pick up an attempt to remove 4-or-so-spaces keeps me
going back to the simple "a tab is an indent" approach. The simple fact
( ;) I know, it's a holy war, there are no facts, but I feel like
tweaking the spacies) is that having 3 or 4 or 2 or 8 characters
represent the concept "indentation level" is just silly when you've got
the single character representation available.

Oh, and decent editors (hint: PythonWin rules), that is, those based on
Scintilla (almost every Python-specific IDE) *do* have a character for
displaying <tab>, it's an elongated arrow that looks surprisingly like
the arrow on my keyboard's tab key :) . BTW the fact that I can set the
display of an indentation level (tab) to whatever width I like is a
*benefit* :) .

Of course, normally I just tell people telling me to switch "sure, I
could change, but I have more Python code than you[1], so *you* conform"
;) ,
Mike

[1] And, generally speaking, this is true, as anyone who has written
more Python is almost certainly one of us old-timers and would never
think of starting this silly thread *again*... :)

________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
blog: http://zope.vex.net/~mcfletch/plumbing/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top