Which do you use for indentation in Python, tab or space?

Asked 2 years ago, Updated 2 years ago, 80 views

I indented it with a tap when I made the Python code

I'm asking because I heard that it's better to float it in space because the length of the tab is different between editors

How are other Python developers using it?

Can there be a conflict between editors because the tap length is different?

coding-style python indentation conventions

2022-09-21 15:57

1 Answers

PEP-8 Tabs or Spaces? tells you to use space

Spaces are the preferred indentation method.

Tabs should be used solely to remain consistent with code that is already indented with tabs.

Python 3 disallows mixing the use of tabs and spaces for indentation.

Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.

When invoking the Python 2 command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!

I usually use space, but the code that has already written tab says that it is better to keep using tab to maintain consistency.

You can mix space and tab in Python 3, The Python2 shell produces an IndentationError.

>>> def foo():
...     print "I'm Tap!"
...     print "I'm Space!"
  File "<stdin>", line 3
    print "I'm Space!"
                               ^
IndentationError: unindent does not match any outer indentation level
>>> 


2022-09-21 15:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.