Why this simplest code is fine with python2 but is wrong with python3?
I do not know python.
I am doing a basic course that uses python, but it does not requires programming knowledge in it, or any other language - although they will help a lot. Basically, this means i skip a few things the course teaches. Anyway, it is giving me very basic examples of python code. Right now, i am in this situation. First, i have a CSV data file: Code:
Cidade, Estado Code:
#!/usr/bin/python Anyway, i ended up finding that my usual "programming habits" are also possible with python, although the course did not mention it. I did not have to install python 2 or 3 in my Debian 9. Both were already installed. 2.7 and 3.5 - and i did not feel a need to update that, since i have done it in very few weeks (maybe days) ago. I installed just the IDLE Python (for both python versions). So, using the terminal with vim to edit the source (with my usual settings for indentation and everything else), and compiling with simple commands, i have this output, after saving the two files above in the same dir: Code:
$ python2 code.py Please notice that the indentation i use with Vim is 4 spaces sized, but tabs are used to transform 8 consec. spaces into 1 tab. Directly, the vim settings i edited *and copied* the source code given above, are: Code:
set noexpandtab # this line is not even in my rc, and not anywhere else What is wrong with me (probably) or python3? |
In vim, you can use "set list" to see which parts are indented with tabs and which are indented with spaces.
Pick one of the two. Python stops you if you're inconsistent. |
It's not the size of tabs, it's the mixing.
Your code has: Code:
for varLine in varReadFile: Pick either tabs or spaces and indent with your choice consistently. i.e either: Code:
for varLine in varReadFile: Code:
for varLine in varReadFile: |
python3 is not intended to be perfectly compatible with python2. If you want to use python3 style printing, add:
Code:
from __future__ import print_function |
The way he's calling print is compatible with both.
|
Quote:
Code:
# I may have Code:
#!/usr/bin/python |
Quote:
Most Python 2 linters (like flake8) would have flagged it as something to be fixed. |
Quote:
|
The problem with mixing tabs and space is that tabs are open to interpretation by an editor.
I always disliked the use of indentation for block demarcation. |
Quote:
Anyway, vim has enough features for that. I will use it in python files, starting today, ts=4 sts=4 sw=4 et=no Anything missing? (any common issue, even not related with indentation, which is the heart of this thread?) |
Quote:
Now, with what i said in #10 with my tabs sized 4 instead of 8, my python sources *will* look different if opened in Windows' notepad.exe or in simpler linux editors like nano, pico, pluma or several others. But they will keep the indentation correct, for their programming block point of view. I like the idea of blocks by indentation. I find it a graceful solution. (: We may discuss the subject i bit more, if anyone wants. I will mark the thread as solved, though, since the problem is already found: python 3 does not accept indentation done with any kind of mix of tabs and spaces. It must be done with one of them, and only one in each whole file. Thank all people who have helped me here, either with help comments or with other words on the subject. |
Code:
et=no Save the following as ~/.vim/after/ftplugin/python.vim Code:
setlocal tabstop=4 shiftwidth=4 expandtab |
The official word, btw:
Quote:
Quote:
|
:retab
Will convert all tabs/spaces to the flavor you are using, it unmixes the mixup. |
Quote:
Quote:
Indentation for clarity in a free-form language, yes. Definitely. Indentation that done incorrectly causes syntax errors. Not! |
All times are GMT -5. The time now is 12:19 PM. |