0

I'm trying to run the following code in python3, but it has been written for I'm pretty sure python2:

f = open(filename, 'r')
self.lines = f.readlines()
f.close()
if self.lines[-1] != "\n" :
    self.lines.append("\n")

But I'm getting the following error:

  File "randline.py", line 32
    if self.lines[-1] != "\n" :
                              ^
TabError: inconsistent use of tabs and spaces in indentation

Can you help me figure out the correct syntax?

3
  • 6
    use show whitespace in your editor. Commented Oct 15, 2011 at 3:11
  • 7
    remove all tabs. Use 4 spaces Commented Oct 15, 2011 at 3:21
  • thanks that fixed my problem. add your comment as an answer so I upvote and accept! :) Commented Oct 15, 2011 at 5:35

1 Answer 1

6

Python 2 allows you to mix spaces and tabs. so you can have indentation like:

def foo():
[this is a tab it counts like eight spaces             ]for each in range(5):
[this is a tab it counts like eight spaces             ][space][space]print(each)
[space][space][space][space][space][space][space][space]print("Done!")

Line 2 and line 4 will in Python 2 have the same indentation level, but line 2 will do it with a tab, and line 4 will do it with spaces. Printed to the console, it will look like this:

def foo()
        for each in range(5):
          print(5)
        print("Done!")

But most editors allow you to set how many spaces a tab should be. Set it to four, and you get this:

def foo()
    for each in range(5):
      print(5)
        print("Done!")

The indentation is still the same, but now it looks like the indentation is wrong!

Python 3 therefore do not allow the same indentation level (ie line 2 and 4) to be indented in different ways. You can still mix tabs and spaces, but not in the same indentation level. This means that

def foo():
[this is a tab it counts like eight spaces             ]for each in range(5):
[this is a tab it counts like eight spaces             ][space][space]print(each)
[this is a tab it counts like eight spaces             ]print("Done!")

will work, and so will

def foo():
[this is a tab it counts like eight spaces             ]for each in range(5):
[space][space][space][space][space][space][space][space][space][space]print(each)
[this is a tab it counts like eight spaces             ]print("Done!")

The only way to make that the only way you can make the indentation look weird is of you set a tab to be more than eight spaces, and then the indentation not only looks obviously incorrect, you'll notice that a tab will indent 12 spaces (in the below example) so you realize you insert a tab, and not four spaces.

def foo():
            for each in range(5):
          print(each)
            print("Done!")

Of course, the solution to all your problems is as written in the comments, to never use tabs. I'm not sure why Python 3 still allows tabs at all, there is no good reason for that, really.

Sign up to request clarification or add additional context in comments.

2 Comments

Great article, until the last (troll bait) clause. There are lots of good reasons for tabs, such as coding in proportional width fonts, or consistency with other language, or having a single character map to a construct, or easier editing in editors that don't honor four spaces as indent as a single construct. There are other reasons too, which is why the subject was contentious for so long, and why I still code in tabs even though I acknowledge that ship has sailed for Python.
Also, today I learned that Python 3 does allow mixing of tabs and spaces within a file, just disallows mixing within a given block (as you stated correctly).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.