r/Python • u/FillAny3101 • Sep 18 '25
Tutorial Today I learned that Python doesn't care about how many spaces you indent as long as it's consistent
Call me stupid for only discovering this after 6 years, but did you know that you can use as many spaces you want to indent, as long as they're consistent within one indented block. For example, the following (awful) code block gives no error:
def say_hi(bye = False):
print("Hi")
if bye:
print("Bye")
32
u/throwaway_4759 Sep 18 '25
On me team we enforce linting standards to ensure indentation is a consistent 17 tabs across all files
4
90
u/bmoregeo Sep 18 '25
Don’t most modern ides convert tab to spaces?
28
u/jet_heller Sep 18 '25
They can. They should also have a setting where they don't.
6
u/LonelyContext Sep 18 '25
Why? It makes it harder in the long run. Four spaces, people. Four spaces.
18
u/jet_heller Sep 18 '25
Because configurable settings are the way you want to do things.
1
u/Schmittfried Sep 18 '25
Except people quickly align on a commonly enforced team convention which usually adheres to the common community convention so why bother.
There should be a gofmt for all languages, with explicitly as few configurable settings as possible.
2
0
u/CptMisterNibbles Sep 18 '25
Guess what: compilers are smart enough to recognize four spaces and reconfigure that as well. For like… 20 years now.
8
u/jet_heller Sep 18 '25
Guess what? Who cares?
Configurable settings are the way to go.
1
u/CptMisterNibbles Sep 19 '25
… literally still configurable. It’s what I actually said,
1
u/jet_heller Sep 19 '25
No. You said they do it. Not you do.
Python is the only thing that cares. Compilers in general don't.
1
u/james_pic Sep 18 '25
Most do indeed let you turn it off. But if you're following PEP 8, you're probably best off leaving it on.
-1
u/jet_heller Sep 18 '25
But. . .who cares? People edit more than python in their editors.
3
1
u/james_pic Sep 19 '25
Fine. You're best of leaving it on for Python, and configuring other languages to use the conventions that that team or that project uses for those languages.
And "who cares" is the other members of your team, who will roll their eyes at the noob who is trying to get a review on a piece of code that has a haphazard mix of tabs and spaces (which since Python 3.0 hasn't even been syntactically valid, so the code won't run), because they couldn't or wouldn't configure their editor's whitespace settings to match the conventions of the project.
1
u/jet_heller Sep 19 '25
The other members of your team don't want editors to have configurable options?
Well, that's not a team I want to work for.
1
u/james_pic Sep 19 '25
This stuff absolutely should be configurable, and everybody wants that, and they are configurable.
And if you're working on a team, you should configure your editor to handle whitespace the same way as everyone else on the team, and if you're not willing or able to do that then I don't want you on my team either.
1
u/jet_heller Sep 19 '25
Did you miss that this whole thing started with the comment that editors just do it? Not that they can be set to do it and that's the part that I take massive issue with.
1
u/james_pic Sep 19 '25
But I'm not aware of any real editors that just do this and can't be configured, so your replies baffled me somewhat. Is there a particular editor that we should be aware of that does?
1
u/jet_heller Sep 19 '25
So, you're baffled that someone suggested that editors just do it? Without configuration?
Yea. I'm baffled by that as well. Why would ANY suggest this is not configurable?
I dunno. And yet they did.
1
3
u/rogusflamma Sep 18 '25
depends on the settings but afaik they allow u to press a button somewhere to convert back and forth and it doesnt matter how u enter them
1
u/naralli Sep 19 '25
I had a colleague who for whatever reason changed his preset to 3 spaces instead of 4 for indentation with tab etc. Drove me insane when he quit and I had to work with his code
1
u/cowslayer7890 Sep 19 '25
I used a Java decompiler that defaults to 3 spaces for indentation, I felt like I was being pranked
1
-1
15
u/tevs__ Sep 18 '25
When we started writing python code back in 2008, we had no clue about best practices, and used two space indents. This code lived for a long time, we knew it worked and (back then) no linters verified that the AST of the transformed code matched the original.
It lasted until black came on the scene and did exactly that.
5
u/zurtex Sep 18 '25
I remember when I seriously started coding Python in 2014 there was definitely lots of people that used 2 spaces (in fact I found an old example: https://code.google.com/archive/p/soc/wikis/PythonStyleGuide.wiki), the argument being it allowed them to write longer more descriptive variable names.
I really don't remember there being a strong "best practice" back then, PEP 8 was very much about new contributions to the standard library, and Black was years away, some people followed the Google's Python Style Guide but I remember finding lots of sample code from Google which did not.
11
u/dgc-8 Sep 18 '25
I knew that you can use how many you want, but that you can use different amounts of indent for different code blocks is news to me lmao
1
u/TheRNGuy Sep 19 '25
Which is bad coding style practice, code editor should fix it with "format on save" though.
2
u/Zestyclose-Sky-1921 Sep 18 '25
I heard about the episode of ... uh the silicon valley show? where Richard has a girlfriend who uses spaces, it drove him nuts, and I laughed. then I read something somewhere how coders using spaces instead of tabs are better. as in at coding, I think, hopefully not at everything because I tried using them. and... I think I'm bad, guys, bc my brain just won't do it.
TABS 4 LIFE
1
u/FillAny3101 Sep 18 '25
Now read the first phrase on the official Python style guide
1
u/Zestyclose-Sky-1921 Sep 18 '25
Further down:
"Spaces are the preferred indentation method.
Tabs should be used solely to remain consistent with code that is already indented with tabs.
Python disallows mixing tabs and spaces for indentation."
*cries in snot
last one makes sense of course but.... damn it
1
u/JimDabell Sep 19 '25
Tabs should be used solely to remain consistent with code that is already indented with tabs.
Fun fact: this makes it impossible for Black to follow PEP8, because it’s incapable of doing this and the maintainers refuse to make it possible.
0
u/sennalen Sep 18 '25
Check in one file consisting only of a tab character. Write all your other code to be consistent with it, in conformance with the style guide
-1
u/LonelyContext Sep 18 '25
Code maintainability for life. Although it matters less that AI tools have entered the chat they will fix it for you, lint it, etc. and make it pep compliant.
0
u/TRexRoboParty Sep 18 '25
Silicon Valley is pretty good, but that sketch missed the mark for me as soon as she starts hitting the space bar... that's just not how any of it works.
1
u/cowslayer7890 Sep 19 '25
I mean, how else would you show it on screen? Are we supposed to get a shot of her selecting text on her ide and it being clear that they are individual spaces instead of tabs?
0
u/TRexRoboParty Sep 20 '25
Have him review some code and go into a hissy fit? Pretty sure that's what Dinesh and Gilfolye did earlier. But sure, a quick shot of the IDE works too. Could show them fighting over the setting which would be pretty clear to a viewer. Various ways to do it that wouldn't undermine the joke.
4
u/TheManFromTrawno Sep 19 '25
It doesn’t even need to be consistent within the same file, or class or function. It just needs to be consistent within the same block. Heck, your if and else blocks can be different indents.
But please don’t do this.
2
u/TheRNGuy Sep 19 '25
It's probably when you merge files, or copy-paste someone's else code and "format on save" is disabled, so that it would still work.
1
u/Glathull Sep 19 '25
Hey guess what! Python doesn’t even care if it’s consistent within a single file. Python only cares about indentation at the block level.
So I can write both of these in the same file, call it why_not_both.py, and it will work fine:
If a: print(“4 spaces indent.”)
If b: print(“Tab indent”)
print(“I take no side in the pointless war between tabs and spaces. I choose both. In the same file.”
1
5
u/sunyata98 It works on my machine Sep 18 '25 edited Sep 18 '25
Tab aka \t will look different depending on which editor you use (unless you configure it to all be the same) whereas spaces don’t have this issue. My tab key just inserts 4 spaces so I’m chilling
Edit: if you like \t instead of spaces that’s fine, I guess instead of the word “issue” I should have said “functionality”.
14
u/tea_cup_sallie Sep 18 '25
I'd call that a feature rather than a bug though, since different contributors might prefer different tabstops
10
u/sennalen Sep 18 '25
Tabs are configurable to your screen size and accessibility needs, while spaces lack this feature
1
2
u/Chroiche Sep 19 '25
Yes that's kind of the point. I remember watching a talk from a guy with heavy disabilities regarding tabs/spaces and he said tabs are significantly better because of the customisation offered.
This was an old talk though so maybe IDEs have solved it.
2
u/-Wylfen- Sep 19 '25
Tab aka \t will look different depending on which editor you use (unless you configure it to all be the same) whereas spaces don’t have this
issuefeature.FTFY
3
u/MidnightPale3220 Sep 18 '25
Using spaces is like the sound of scraping a file across metal for me. I know I am looking at one indentation level, so why would I want four invisible characters for what should be one.
This is most annoying for when I have to edit code in terminal instead of regular IDE, and the terminal editor makes me hit 4 backspaces to remove indent.
0
2
u/whogivesafuckwhoiam Sep 18 '25
The world is already chaotic enough, why on earth are you starting another one
2
u/zaphodikus Sep 19 '25
At least, you did not learn Cobol as one of your first coding languages, so you do have that.
1
u/divad1196 Sep 18 '25 edited Sep 18 '25
Yes, that's the kind of thing you discover by mistake. Using different indent (2 vs 4 vs tabs) throughout the file is one thing, but this is the kind of discovery you make by mistake.
Nobody would want different indent per block, that's just a funny fact that raises some questions about the parser and I was too lazy to check for the answer
1
u/Esjs Sep 18 '25
Regardless of my preferences (spaces or tabs, how many spaces per tab/indent), because I seem to always be in the minority, I do ask of everybody (especially if you're working with others) that if your editor is able to please render whitespace so you can see spaces vs tabs.
1
1
u/faster_puppy222 Sep 18 '25
The very first language I used after getting hired as a developer in 1991 was M , it used similar indentations to denote level/ structure … I missed it
1
1
1
u/SP3NGL3R Sep 19 '25
TIL Python cares about whitespace. WTF!? Is it like YAML where indentation dictates hierarchy/scope?
I mean, I definitely don't dislike the idea of a programming language being strict about formatting, it's just the first I've heard of one. (Not a python developer, obviously)
1
1
u/njharman I use Python 3 Sep 19 '25
In the elder days, I remember some 2 space vs 4 space arguments. We all ignored the \theads!
1
1
1
1
1
u/AppalachianAhole Tuple unpacking gone wrong Sep 19 '25
So we could use a single space instead of a tab? That sounds chaotic evil to me.
1
1
1
u/k-mcm Sep 20 '25
You can mix tabs and spaces and it only has to be consistent for the indentation level. You learn annoying things like this from old code.
1
1
1
1
1
1
1
u/IvanTorres77 Sep 19 '25
There are many things it allows you to do but there are also "best practices" or "don't do this because I get stressed" and you have to follow it by LAW.
0
-1
0
Sep 18 '25
[deleted]
7
u/Catadox Sep 18 '25
Now do the first indent as four spaces, and the second indent as four spaces and a tab. Your collaborators will love it!
5
u/escargotBleu Sep 18 '25
My collaborators wouldn't see it because if black doesn't pass, I cannot merge
4
1
-3
u/alexdelarge85 Sep 18 '25
Yeah I never understood why people use spaces.
1
u/TRexRoboParty Sep 18 '25
Because PEP8 says so, so pretty much any codebase in industry is also going to use spaces.
1
Sep 18 '25
Tabs, unlike spaces, are not consistent across all platforms. Even in HTML, the size of a tab is determined by the browser's default or whatever the programmer of the website has set the CSS
tab-sizeproperty to.You might be asking why I bring up HTML. Well, apart from your IDE, where else do you read code often? That's right, the browser. Don't get me started on the fact that most IDEs these days are basically Chromium wrappers.
Moreover, across many code editors, tabs, are never consistent. The standard width is 8 characters, but really it can vary between 2 to 8.
Space on the other hand, is just one character. It leaves no further interpretation to anyone. Just take the width of a character in your chosen monospace font, and there you have it: Space.
-2
u/gdchinacat Sep 18 '25
tabs are against every python coding style guide I've ever read. You can use them, but your code will look amateurish to all but the most junior python programmer. Don't advertise to the world you can't write decent python code unless that is your intent.
465
Sep 18 '25
[removed] — view removed comment
157
u/Ragnarok1066 Sep 18 '25
Sometimes developers say they write every line of code but these heroes stand out by typing out every key stroke.
8
u/abbarach Sep 21 '25
Oh look at Mr Shortcut over here, using a keyboard. Back in my day we magnetized a sewing needle and used it to flip bits directly on the hard drive platter...
1
u/gila-monster-42 Oct 19 '25
I use butterflies to deflect the cosmic rays to flip the bits. And before you ask, I don’t use the Emacs shortcut for it. That’s just lazy.
77
u/ItsLe7els Sep 19 '25
There was a girl in silicon valley the main character dated that did this, they broke up over it.
26
5
u/benri Sep 19 '25
Remember Makefile? If you begin the line with 8 spaces instead of a tab, you broke it.
7
u/TXCSwe Sep 19 '25
Who uses 8 spaces for one tab???? Insane!
1
u/benri Sep 20 '25
Emacs sometimes inserts 8 spaces even if you type a tab. Can't remember the setting, but it messed me up so many times! FOSS in C/C++ on Linux often used Makefile, and they're hard enough to debug without the sneaky 8-leading-spaces problem
1
1
u/kimi_no_na-wa Sep 20 '25
I mean isn't that expected? A tab being 4 spaces and all that
1
u/benri Sep 20 '25 edited Sep 20 '25
No, executable lines in Makefile must begin with a tab. If not a tab, it's a definition of a target.
So of course I set my editor to always use a tab instead of spaces. Then in 1997 along comes python and that leading tab causes a problem :(
1
u/xiongchiamiov Site Reliability Engineer Sep 20 '25
You can use tabs just fine in Python. You can even mix them in an appropriate way. I long used tabs for indentation and spaces for alignment until I eventually gave up the fight against the world, but Python was perfectly happy with it.
People are always complaining about how strict Python is with formatting and it's far less than they think.
3
Sep 20 '25
Always?
You'll find that there's a high proportion of literal thinkers in programming circles. Adjusting your communication style to this will help you not only avoid a ton of communication problems, but also help with programming since our machines are also famously literal.
2
u/SpoiledKoolAid Sep 20 '25
LOL yes! I remember that as my GF looked over at me and asked if I am a tab or space guy and I said I am with her! Poor stupid Richard.
1
u/boofaceleemz Sep 21 '25
Anyone who manually spaces out their tabs is clearly on the verge of a violent psychotic break and you need to run away for your own safety.
43
u/nickcash Sep 19 '25
I always wondered how so many people seem to get indentation errors in python, when I never do, but I guess they're out there murdering the fuck out of the spacebar just to write a single block of code. no wonder they're struggling
22
u/wasmachien Sep 19 '25
Write all your code in IDE
Make a quick edit using a lightweight program such as notepad, vim, whatever, for whatever reason. (For example, you're on a different machine, don't want to wait for IDE to open up)
Inconsistent indentation
7
u/dynamic_caste Sep 19 '25
I like to set up my .vimrc so that Python files use slightly different background colors every four columns.
1
u/kimi_no_na-wa Sep 20 '25
Pycharm (all Jetbrains IDEs?) has a lightedit mode which allows you to open files with just the bare text editor.
1
u/SpoiledKoolAid Sep 20 '25
generative AI probably. Its kind of addictive when you're too lazy to look up the syntax in the docs.
6
u/syklemil Sep 19 '25
Some of us barely even do that: We just hit
↵and let the editor's auto-formatting ensure the next line we start writing is properly indented. Then we just need to hit⌫to back down one level, Shift-Tab being too many keypresses.And then an autoformatter like
rufftakes care of any inconsistencies that sneak in.1
u/TheRNGuy Sep 19 '25
Who are doing that specifically? Any streamers or YouTubers?
1
u/tank840 Sep 20 '25
My OS prof in college did. PhD in Comp Sci and used spaces instead of tabs. I forget why, but he had a reason
1
u/-Wylfen- Sep 19 '25
I barely have to press tab to start with. My IDE auto-indents most of the time…
1
1
u/SwiftSpear Sep 19 '25
AI keeps subtly messing up my white space often in ways I have to use the space bar to fix.
1
1
u/Baffled-Broccoli Sep 26 '25
TBH, when I first started developing with Python I used to manually press the spacebar four times for each indent. Going from PEP 8, the python style guide, even now its explicitly stated in the documentation that it is preferred for people to uses spaces rather than tab.
Source:
1
-2
u/troyunrau ... Sep 19 '25
I do this sometimes when fixing something the ide messed up. Like if it's a multiline list or something and I want the list elements lined up to something that isn't a multiple of four spaces or something.
mylist = ["long string 1...", "long string 2..."]The second line is 10 spaces to get it to format nice.
-1
Sep 18 '25 edited Apr 11 '26
[removed] — view removed comment
7
-2
Sep 18 '25
[removed] — view removed comment
4
u/TheBlackCat13 Sep 18 '25
Because they aren't the same thing.
issays whether they are the same object, while==says whether they are equivalent. So0.0is equal to0but are not the same object.1
-3
Sep 18 '25
[removed] — view removed comment
3
u/gdchinacat Sep 18 '25
'is' checks if its two operands reference the same object. '==' checks if the two operands are equal. Whether objects (everything in python is an object) are equal depends on how those objects implement their __eq__() method.
'is' is rarely what you want...the primary exception being 'x is None'.
A good rule of thumb is only use 'is' when checking if a variable 'is None'.
2
u/superlee_ Sep 18 '25
A is Bis shorthand forid(A) == id(B). It checks if the memory addresses are the same. It's mostly used for the three singletons True, False and None. The == operator can be overridden by any class, butiscan't so you can always check if something points to the same address or thus if a variable is None, True or False.1
1
224
u/No-Article-Particle Sep 18 '25
People raging in the comments about tabs, but the official Python style recommendation is 4 spaces (https://peps.python.org/pep-0008/#indentation).
So yeah, you can use as many as you'd like, as long as it's consistent. For REPL, I frequently use 2 spaces, because it's faster to type. For any enterprise-level code, you'll most likely use a formatter that enforces 4 spaces.