Indentation character in a script

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Indentation character in a script

Post by liudr » Fri Apr 13, 2018 4:46 pm

I am using Python 3 IDLE on all my computers. The indentation is 4 spaces whether you press TAB or Ctrl - ]. I noticed that MicroPython script needs the TAB character otherwise it will not understand indentation correctly. So are the 4-spaces tabs not recognized by MicroPython?

I have not found any way for IDLE to produce TAB characters so I'm using Npp for MicroPython scripts. It's a bit annoying. I like to test snippets of code on PC in IDLE and copy the snippets to MicroPython script for upload. I have to manually strip the spaces IDLE produces and replace with TAB characters in Npp.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Indentation character in a script

Post by Roberthh » Fri Apr 13, 2018 8:04 pm

I use indentation by spaces throughout all editors, which works well with all Python variants. I set my editors to replace Tab by spaces. Maybe you can check with hd or similar which characters are really in your scripts.

User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: Indentation character in a script

Post by liudr » Fri Apr 13, 2018 8:11 pm

I'll check again but my impression is only tab character works. Thanks.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: Indentation character in a script

Post by OutoftheBOTS_ » Fri Apr 13, 2018 8:23 pm

I use note++ to write all my micropython scripts and only use double space for indentation by pressing space bar twice.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Indentation character in a script

Post by stijn » Sat Apr 14, 2018 6:34 am

liudr wrote:
Fri Apr 13, 2018 4:46 pm
I noticed that MicroPython script needs the TAB character otherwise it will not understand indentation correctly.
Either this is a bug, or you are accidentally mixing tabs with spaces leading to visually correct indentation, which actually isn't correct. Can you give an example of code which can be used to reproduce this? As others said, MicroPython normally handles spaces just fine.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Indentation character in a script

Post by pythoncoder » Sat Apr 14, 2018 11:16 am

MicroPython undoubtedly works with four spaces. It is the PEP8 Python coding standard.
Peter Hinch
Index to my micropython libraries.

User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: Indentation character in a script

Post by liudr » Sat Apr 14, 2018 7:53 pm

stijn wrote:
Sat Apr 14, 2018 6:34 am
liudr wrote:
Fri Apr 13, 2018 4:46 pm
I noticed that MicroPython script needs the TAB character otherwise it will not understand indentation correctly.
Either this is a bug, or you are accidentally mixing tabs with spaces leading to visually correct indentation, which actually isn't correct. Can you give an example of code which can be used to reproduce this? As others said, MicroPython normally handles spaces just fine.
Quite possibly I mixed tabs with spaces. I carefully wrote a test program with only 4-space indentation. Works fine with 3 levels of indentation!

I feel relieved. It's just me again! I will see if I can make Npp do 4-space when I press tab key. Using Npp on an existing python script out of IDLE was probably the cause. Npp uses tab characters while IDLE uses 4-space.

Thanks as always.

[Edit]: There is a general setting in Npp to replace tab with spaces and language-specific setting.
Here is what I had to do:
Image

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Indentation character in a script

Post by stijn » Sun Apr 15, 2018 6:45 am

Note if you use View->Show Symbol->Show White Space and TAB it will show the characters.
Also if you don't ever want to have problems like this again make the computer fix it for you, that is what computers are for, not the other way around. Most text editors can automatically turn tabs into spaces at save/load of a file. I don't use npp anymore but this should do it, for example: https://github.com/editorconfig/editorc ... -plus-plus.

Post Reply