On 2019-12-05 19:30:31 +0000, Rhodri James wrote: > On 05/12/2019 18:49, RobH wrote: > > TabError: inconsistent use of tabs and spaces in indentation > > The problem will be that you have a mix of tabs and spaces in your > indentation. This causes problems because some people don't think that the > One True Tab Width is 8 characters ;-) so to them the indentation looks > ragged. Worse, when they mix tabs and spaces, code that looks to be at the > same indentation level to them looks different to the interpreter. The > decision was taken a while ago that Python should put its foot down about > this, and demand that we use either all tabs or all spaces for our > indentation.
At least as of Python 3.7, this is not true: You can mix spaces and tabs
in the same line. The mix just has to be consistent between lines. So
for example:
1 #!/usr/bin/python3
2
3 def f(a, b, c):
4 if a < b:
5 »·······if b < c:
6 »······· print("x")
7 »·······else:
8 »······· print("y")
(I have configured vim to display a tab as a right guillemet followed by
middle dots and simply pasted that into this message)
produces the error message:
File "./indent3", line 5
if b < c:
^
TabError: inconsistent use of tabs and spaces in indentation
because depending on the tab width line 5 might be more or less indented
than line 4.
(I don't actually understand the rules here: I constructed a very
similar example before that the same python interpreter accepted.)
However, this works:
1 #!/usr/bin/python3
2
3 def f(a, b, c):
4 if a < b:
5 »···if b < c:
6 »··· print("x")
7 »···else:
8 »··· print("y")
I would recommend to avoid that and stick to either tabs or spaces
(personally, I prefer spaces). You might want to use an editor which
can display tabs specially (like vim).
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | [email protected] | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
signature.asc
Description: PGP signature
-- https://mail.python.org/mailman/listinfo/python-list
