Discussion:
[Tutor] Pep 8, about indentation
D Wyatt
2015-08-05 23:31:42 UTC
Permalink
I clearly remember that the standard for Python was to use 2 spaces
for indentation of blocks. Now Pep 8 says 4. When did that change?
I hate it when things change on me like that. And what else has
changed?

Then again, maybe I dreamed it. Am I crazy? TIA
--
Deb Wyatt in WA
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Ben Finney
2015-08-06 01:21:37 UTC
Permalink
Post by D Wyatt
I clearly remember that the standard for Python was to use 2 spaces
for indentation of blocks. Now Pep 8 says 4. When did that change?
When do you remember it being as you describe?
--
\ “People's Front To Reunite Gondwanaland: Stop the Laurasian |
`\ Separatist Movement!” —wiredog, http://kuro5hin.org/ |
_o__) |
Ben Finney

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/m
Alan Gauld
2015-08-06 01:24:49 UTC
Permalink
Post by D Wyatt
I clearly remember that the standard for Python was to use 2 spaces
Nope, unless it was a local convention in your place of work.

But the standard for indentation has always(*) been 3 or 4 and fro PEP 8
its 4. And thats pretty standard in any language because its the size of
indent that gives best clarity to the code. (Based on tests of students
reading code samples.)

(*) "Always" since the mid 1980s at least. Certainly
before the advent of Python.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-08-06 03:40:08 UTC
Permalink
Post by Alan Gauld
Post by D Wyatt
I clearly remember that the standard for Python was to use 2 spaces
Nope, unless it was a local convention in your place of work.
But the standard for indentation has always(*) been 3 or 4 and fro PEP 8
its 4. And thats pretty standard in any language because its the size of
indent that gives best clarity to the code. (Based on tests of students
reading code samples.)
Um, the oldest standard for TABs is *eight* spaces, not 3 or 4, and
using eight-space indents is still very common. Vim, apparently,
defaults to 8 space indents when you hit the tab key. Gofmt (used by the
Go language) defaults to 8 spaces; even Python does in places:

expandtabs(...)
S.expandtabs([tabsize]) -> str

Return a copy of S where all tab characters are expanded using
spaces. If tabsize is not given, a tab size of 8 characters is
assumed.

The Linux kernel developers use 8 space indents, and consider that
important enough that it is the very first item in their style guide:

https://www.kernel.org/doc/Documentation/CodingStyle

For interest, here's a survey done by Perl programmers in 2002:

http://www.perlmonks.org/?node_id=158886
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-06 10:03:38 UTC
Permalink
Post by Steven D'Aprano
Um, the oldest standard for TABs is *eight* spaces, not 3 or 4,
Yes, but that comes from the days of mechanical typewriters not any
study of code comprehension. I was referring to the recommended spacing
for comprehending code. There were a lot of studies done on this back in
the 70's and 80's. The only one I can find a reference to right now is
an Article from 1983, "Program Indentation and Comprehensibility" by
Miaria et al. It showed a 20-30% improved comprehension based
on 2-4 tabs. 0-1 space tabs and larger tabs decreased the
comprehensibility of the code.
Post by Steven D'Aprano
http://www.perlmonks.org/?node_id=158886
Yes, but that's a survey of what developers prefer to use. It's not a
study of whose code is most reliably read and understood!
(Which in the case of Perl might not be very high for any style!!)
Developers are notoriously bad at choosing the most readable
standard, they tend to go by what they think looks pretty.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Cameron Simpson
2015-08-07 08:18:39 UTC
Permalink
Post by Alan Gauld
Post by Steven D'Aprano
Um, the oldest standard for TABs is *eight* spaces, not 3 or 4,
Yes, but that comes from the days of mechanical typewriters not any
study of code comprehension.
[...snip...]

I beg to differ a little. Old _teletypes_ and terminals used 8 character TAB
spacing.

Mechanical typewriters had slidable lugs to position to totally arbitrary spots
with a lever motion triggered by a key labelled "Tab", which was very useful
for typing tabular information, hence the name. There's no "8" in there!

My personal habit is to have my terminals, editors such as vi, pagers like less
etc interpret the TAB byte code to mean align to the next 8 column position.
That is the common default meaning.

However, when _editing_ I tell vi that when I press TAB it is to insert enough
SPACE characters to get to the next 4 column position. In this way I have the
convenience of a TAB key to advance rapidly when needed, and my code comes out
composed entirely with spaces so that my spacing in not mispresented to others.

In short, I use the TAB _key_ to aid typing. But I avoid embedded TAB
characters into my text, because they are subject to arbitrary interpreation by
others.

Cheers,
Cameron Simpson <***@zip.com.au>

I had a *bad* day. I had to subvert my principles and kowtow to an idiot.
Television makes these daily sacrifices possible. It deadens the inner core
of my being. - Martin Donovan, _Trust_
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-07 17:48:30 UTC
Permalink
Post by Cameron Simpson
Post by Alan Gauld
Yes, but that comes from the days of mechanical typewriters not any
study of code comprehension.
I beg to differ a little. Old _teletypes_ and terminals used 8 character
TAB spacing.
Quite right, I realized after I posted that I'd said typewriter
instead of teletype. My background is that I started in Telex
and the old mechanical teleprinters which had hard coded tabs
(and separate linefeed and carriage return keys)...

But I also had a very cheap Remington portable typewriter with
fixed tabs of 8 chars too. But it was indeed teletypes/terleprinters
that I was thinking about. And they still pre-date coding by a long way.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alex Kleider
2015-08-07 19:19:07 UTC
Permalink
Post by Cameron Simpson
However, when _editing_ I tell vi that when I press TAB it is to insert enough
SPACE characters to get to the next 4 column position.
How do you do that?
I've got vim set up so a CTRL-T while in insert mode does that (and CTRL-D does the opposite) but don't know how to make use of the tab key.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Cameron Simpson
2015-08-08 03:56:08 UTC
Permalink
Post by Alex Kleider
Post by Cameron Simpson
However, when _editing_ I tell vi that when I press TAB it is to insert enough
SPACE characters to get to the next 4 column position.
How do you do that?
I've got vim set up so a CTRL-T while in insert mode does that (and CTRL-D does the opposite) but don't know how to make use of the tab key.
^T and ^D have done that since time immemorial in vi, no special setup
required. I pretty much never use ^T but I do use ^D to back out an indent
level.

These are my settings relevant to tabs and indenting:

set autoindent
set expandtab
set shiftwidth=2
set tabstop=4

In order:

autoindent: start the next line's text on the same indent as this one

expandtab: write spaces instead of a TAB character

shiftwidth: how far the < and > shift-text operations move

tabstop: the multiple used to tabstops - every 4 columns for me

Happy to post my other settings should anyone care.

Cheers,
Cameron Simpson <***@zip.com.au>

Swiftkey, [...] teaches one the lamentable lesson that most
English speakers start most sentences, phrases and sub-clauses
with 'I'. One does not use the word often oneself and it therefore
strikes one as a little unfair that one's texts and emails so often
end up littered with implications of egocentricity. - Stephen Fry
http://www.stephenfry.com/2012/04/03/four-and-half-years-on/9/
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alex Kleider
2015-08-08 04:50:46 UTC
Permalink
Post by Cameron Simpson
Post by Alex Kleider
Post by Cameron Simpson
However, when _editing_ I tell vi that when I press TAB it is to insert enough
SPACE characters to get to the next 4 column position.
How do you do that?
I've got vim set up so a CTRL-T while in insert mode does that (and
CTRL-D does the opposite) but don't know how to make use of the tab
key.
^T and ^D have done that since time immemorial in vi, no special setup
required. I pretty much never use ^T but I do use ^D to back out an
indent level.
set autoindent
set expandtab
set shiftwidth=2
set tabstop=4
autoindent: start the next line's text on the same indent as this one
expandtab: write spaces instead of a TAB character
shiftwidth: how far the < and > shift-text operations move
tabstop: the multiple used to tabstops - every 4 columns for me
Thanks!

Here is what I've got in my ~/.vimrc file:
set autoindent
set shiftwidth=4
set expandtab
set textwidth=72
set scrolljump=2
set scrolloff=2

I'll add
set tabstop=4
Post by Cameron Simpson
Happy to post my other settings should anyone care.
... and yes, I for one certainly do "care."
I'd very much like to see your other settings.

Cheers,
Alex
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Cameron Simpson
2015-08-08 07:14:22 UTC
Permalink
Post by Alex Kleider
Post by Cameron Simpson
autoindent: start the next line's text on the same indent as this one
expandtab: write spaces instead of a TAB character
shiftwidth: how far the < and > shift-text operations move
tabstop: the multiple used to tabstops - every 4 columns for me
Thanks!
set autoindent
set shiftwidth=4
set expandtab
set textwidth=72
set scrolljump=2
set scrolloff=2
I'll add
set tabstop=4
Post by Cameron Simpson
Happy to post my other settings should anyone care.
... and yes, I for one certainly do "care."
I'd very much like to see your other settings.
Ok. Here are my other vi settings which I think useful (it seems I have plenty
which aren't). Vim specific settings follow after that.

set autowrite
Automatically save the file when switching to another. Saves tedious :w and
also worrying about whether I should, not to mention all those "file
modified" warnings.

set ignorecase
Saves fiddling with shift keys.

set nowrapscan
Searches do not resume from the top of the file if nothing found below the
current position. I used to have this on, but now consider this more
informative.

set optimize
set redraw
I forget, probably dates from editing over a 300baud modem.

set report=1
Threshold for reporting changed lines.

set showmatch
Highlight matching open/close bracket. Handy for nested stuff. And C code.

set noterse
Longer warnings and other messages.

set writeany
Trust me, I'm a doctor. (I'm not.)

map! \c ***@zip.com.au
map! \C Cameron Simpson <\c>
Insert mode macros to recite my email, short and long.
I've got matching ones for my partner's email, very often CCed or
attributed.

map!  :stop
a
Suspend the editor, just as with ^Z in the shell.

map!  a
Redraw the screen then resume inserting.

map!  I
map!  i
map!  A
map!  lli
Emacs mode motion: start of line, left, end of line, right, resume insert
mode.

map # :n #
z.
Edit the previous file. Very very handy.
map g Gz.
Go to line and position it in the centre of the screen.

map q ZZ
Save and quit.

map t :ta
Commence "go to tag".

map Y y$
Yank to end of line.

map  1G}-:.r!exec </dev/tty 2>/dev/tty; readline -B 'Attach: '
IAttach: 
My uber-convenient "attach file" macro. In mutt (my mailer) an "Attach:"
header gets turned into a real attachment. So this macro runs a bit of
shell to issue the prompt "Attach: " and accept the filename you typ, _with
filename completion!_. And inserts that as an Attach: header. When I leave
the editor, mutt adds the file to the attachments for this message.

set bg=dark
My terminals have a dark background.

syntax reset
syntax on
I like my syntax highlighting.

set wildmenu wildmode=list:longest
Make vim's filename completion act more like zsh's, which I find far more
ergonomic than bash's. Essentially: complete as much as you can and
_immediately_ show me the remainder without an extra TAB keystroke.

set ignorecase
set smartcase
Case insensitive searches unless there is an uppercase letter in the search
string.

set incsearch
Show the first match in the file as you type the search regexp.

set backupcopy=yes
I prefer to overwrite the original file instead of making a new one and
cutting it in with rename(). The former edits the file I intended to edit.
The latter breaks hardlinks and has to do silly hoop jumping with symlinks.
Gah! Do what I say, not what you think I want!

set noswapfile
set swapsync=
Vim's fetish for .swp files drives me mad, especially if one loses remote
sessions regularly or terminal emulators die or one coworks with others in
the same directories or simply closes one's laptop one a whim or moves
around. I save files very regularly anyway, so I pretty much never lose a
significant edit.

set nofsync
Trust the filesystem, the filesystem is your friend. Once I've handed the
file to the OS (i.e. written it) I do not want to shout at the OS: not get
that onto the disc, now! Especially when fsync() means sync(0 on many
filesystems i.e. write _all_ pending disc I/O before returning, often with
a huge and visible hit to the system behaviour.

set scrolloff=2
Like you, keep some screen context.

set t_Co=256
Yes, my terminal have lots of colours.

set ttyfast
Better display instead of lower bandwidth display.

set spellfile=~/rc/vim/spellfile.utf-8.add
Additional words to augument the spelling table.

set spelllang=en_gb
British English.

set nospell
Spell checking off by default.

let g:loaded_netrw = 1
I forget. Anyone?

map <F3> :set hlsearch! nospell! <CR>
F3 turns off highlighting.

and when composing email I use:

set formatoptions=wa
This supports writing format=flowed email text. See ":help fo-table".

Cheers,
Cameron Simpson <***@zip.com.au>

Philosophy is questions that may never be answered. Religion is answers that
may never be questioned. - anonymous
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
David Rock
2015-08-10 15:33:15 UTC
Permalink
Post by Cameron Simpson
set autoindent
set shiftwidth=4
set expandtab
set textwidth=72
set scrolljump=2
set scrolloff=2
I'll add
set tabstop=4
You might want to add softtabstop as well.
set softtabstop=4

It's very handy for allowing the delete key to go back TAB number of
spaces (ie, deletes those 4 spaces you just inserted).
--
David Rock
***@graniteweb.com
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alex Kleider
2015-08-10 18:26:54 UTC
Permalink
Post by David Rock
You might want to add softtabstop as well.
set softtabstop=4
It's very handy for allowing the delete key to go back TAB number of
spaces (ie, deletes those 4 spaces you just inserted).
I got it working but the key needs to be the 'backspace' key, not the
'delete' key.
Either way, very handy (replacing the need to use CTRL-D.)
Thanks,
Alex
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
David Rock
2015-08-10 19:15:46 UTC
Permalink
Post by Alex Kleider
Post by David Rock
You might want to add softtabstop as well.
set softtabstop=4
It's very handy for allowing the delete key to go back TAB number of
spaces (ie, deletes those 4 spaces you just inserted).
I got it working but the key needs to be the 'backspace' key, not the
'delete' key.
Either way, very handy (replacing the need to use CTRL-D.)
Yeah, BS is more accurate (although BS says Delete on my keyboard).
Gotta love consistency :-)
--
David Rock
***@graniteweb.com
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Marc Tompkins
2015-08-10 21:34:17 UTC
Permalink
Post by David Rock
Yeah, BS is more accurate (although BS says Delete on my keyboard).
Gotta love consistency :-)
I'm guessing you use a Mac, then...?
Whenever I have to use a Mac keyboard, the lack of a Delete/relabeling of
the Backspace key drives me nuts.

The explanation I've heard is "why would I need to delete what I haven't
typed yet?"
Grrr.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...