[Tutor] Notepad++ question

Dave Angel d at davea.name
Thu Jun 7 06:50:45 CEST 2012


On 06/07/2012 12:29 AM, Alexander Quest wrote:
> Hey all; my question is regarding editing Python code in Notepad++. When I
> run this piece of code in Notepad++:
>
> def fix_start(s):
>   var1 = s[0]
>     var2 = "*"
>   var3 = s.replace(var1, var2)
>
>   return var3
>
>
> I get an indentation error, which reads:
>
>
>   File "C:\google-python-exercises\google-python-exercises\basic>string1.py
> line 56
>     var2 = "*"
>     ^
> IndentationError: unexpected indent
>
>
> The thing is that in Notepad++, that code does not appear with an
> indentation where var2 is. It appears like this:
>
> def fix_start(s):
>   var1 = s[0]
>   var2 = "*"
>   var3 = s.replace(var1, var2)
>
>   return var3
>
> but when I copy and paste it, it pastes with an indentation where var2 is,
> which is what I think is causing the error. The code runs fine if I just
> use IDLE. I am doing Google's python exercises, and they recommended I edit
> the settings on Notepad++ to indent 2 spaces upon a tab, this being the
> convention at Google. Does anyone know what the deal is here? Also, I am
> wondering why use Notepad++ or other such programs when IDLE seems to be
> fine for writing code. Thanks.
>
> -Alex
>
>
I can't tell you what settings to use in Notepad++, but what you want is
either for tabs or spaces to be used for all indenting in a source file,
and never a mixture.  I think if you view your file in a hex viewer,
you'll see an invisible tab character tucked on that one line.

My opinion is to stick entirely to spaces.  30 years ago, when i first
faced this problem, I came up with what seems to be the best answer. 
The editor should not make any connection between the keyboard button
called tab and the file character hex(09).  When you use the tab key to
line up columns, the editor should insert an appropriate number of
spaces to make it come out right.  And if you subsequently want to
convert all the leading characters into one or more tabs followed by one
or more spaces, that should be done to the whole file at once.

Others think one should use tab characters exclusively for indenting. 
While there are some conceptual advantages to that, the biggest downside
is that very few editor/viewers make the tab characters visible.  I used
such an editor decades ago, and it solved most of the problems.

As for IDLE, I've never used it, so I don't know it's limitations.


-- 
DaveA


More information about the Tutor mailing list