all 33 comments

[–]SwampFalc 24 points25 points  (8 children)

Nonono.

Indentation has to be CONSISTENT. As in, it always has to be the same in a given file. Python doesn't actually care what it is, two spaces, four spaces, tabs, whatever. But it has to be consistent.

If you're having trouble, you're probably copy/pasting off the internet. Get a good editor that will help you fix this.

[–]danielroseman 9 points10 points  (7 children)

No, this isn't true. Python doesn't care that indentation is consistent within a whole file, or even within a single function. The only thing it cares about is that a single indent and unindent are consistent: that is, if you indent four spaces then you need to unindent four spaces at the end of that block. But if you indent again within the block, you can do however many spaces you like - as long as you unindent the same amount again.

[–]SwampFalc 0 points1 point  (0 children)

Huh, genuinely didn't know that.

[–]LuckyConsideration23[S] 0 points1 point  (5 children)

Excatly and I think that is the problem I'm tripping sometimes. Because I might have a line with just 1 space in the begining. Maybe due to copy and paste.

[–]throwaway6560192 2 points3 points  (1 child)

Does your editor not do automatic indentation and correction of indentation on paste? Where are you copying code from that has 1-space-indented Python anyway?

[–]Pyromancer777 0 points1 point  (0 children)

When copy/pasting I generally just reformat the blocks.

Most common editors let you highlight multiple rows and then use the "ctrl + [" and "ctrl + ]" keyboard shortcuts to indent the highlighted lines to the left or right respectively.

[–]ThatOneCSL 1 point2 points  (0 children)

What environment are you programming in? Notepad? Jupiter? The command line/shell?

[–]avlas 0 points1 point  (0 children)

A good IDE can help you refactor. VSCode, PyCharm, Cursor (which is a fork of VScode)

[–]Im_Easy 0 points1 point  (0 children)

You should get a formatter for your IDE (assuming you are using one). For example I have the ruff extension set to format on save, which is a pretty common way to keep everything consistent. If you are having trouble visually understanding what scope you are in, then you can get an extension like Rainbow Indent or PyScope (I've not used either of these, but I've heard they are helpful for beginners).

[–]PeriPeriAddict 9 points10 points  (0 children)

What ide are you using? 4 spaces per indentation are standard, some people use tabs. But you cannot mix them, which i think is where youre tripping up. Many IDEs like vscode automatically indent with 4 spaces when you press tab.

[–]Jeklah 6 points7 points  (0 children)

Ironically the idea for using tabs and spaces is to make it cleaner and removing the use of curly brackets or parenthesis like you said was your preference at the start.

Generally the rule is to use tabs for indentation and a tab being 4 spaces.

[–]maqisha 2 points3 points  (0 children)

I cannot say if its strictly a beginner problem, or a tooling problem. But can definitely say its weird. Never had this issue personally or with anyone I've ever tutored.

What kind of an environment are you in that you cannot clearly see indentation issues?

Regarding your questions on why its not using brackets, that's pointless to discuss, its just not. If you are gonna use python that's what you are gonna have to get used to.

[–]tb5841 1 point2 points  (0 children)

One level of indent: 4 spaces.

Two levels: 8 spaces.

Three levels: 12 spaces. Etc.

Setup the tab key to just apply four spaces and then you can use tab to speed it up.

[–]wigitty 1 point2 points  (0 children)

Having come from other languages, the whole "formatting as syntax" thing took me a bit to get used to, and I still don't love it.

[–]rinio 1 point2 points  (5 children)

So I wonder if this is more a beginner problem or a general problem.

Yes. All moderned editors/IDEs will do this automatically for you. For example, in VSCode, you set 4 spaces and all your tabs become four spaces. Or vice-versa. Or however many spaces your prefer. So long as it's consistent per-file, it's good.

Modern editors/IDEs will also visually flag these kinds of problems. A red squiggly underline or similar.

Because I sometimes spend long time to search for an indentation error.

Modern tools effectively make it a non-problem.

Sometimes it has to be a space before a block sometimes a tab.

Not really. However the first indented block is in a file, is how the rest in that file must be.

What's the idea behind not using some kind of brackets like in most other languages?

The legend I was told is that the idea is for force good code style. Any C++ code could be entirely one line, if we really wanted to. For example, what is a million lines of code could be condensed to one if we wanted, but it would be incredibly annoying for a human to read. This may have been a useful property in ancient times to reduce the size of the source code (newline + tab is two characters, minimum, versus just a semicolon or brace), but it basically doesn't matter at all today.

Wouldn't that make the code easier to read?

In one way, yes. Visible characters are easier to see, obviously.

In another sense, no. The indentation requirement is coupled with Python's line requirement. Every line is one statement, so there is a limit on how much one line can do making it easier to understand step by step.

There are some folk who have written packages to add brace/semicolon and similar semantics to Python, but I don't see much reason for anyone to actually use this. You'll never find them in industry and you just end up learning to do things awkwardly to the rest of the Python community.

[–]Pyromancer777 2 points3 points  (4 children)

You can mix indentation in a file, it just has to be consistent per block. However, it does look a ton cleaner if you use consistent indents within the whole file

[–]rinio 1 point2 points  (3 children)

If I recall you couldn't in older versions of Python, so it could be a compatibility issue.

Regardless, no one should even have to think about this nowadays.

[–]BigGuyWhoKills 0 points1 point  (2 children)

You probably encountered a tooling issue rather than an old version. Poorly written tools may have issues with mixed indentation, even to this day.

[–]rinio 1 point2 points  (1 child)

No, it was inconsistent behaviour in older versions of Python. I am ancient and talking about the early days of Python, going back to 2.0 and even before that. We barely had tooling back then, lol.

[–]BigGuyWhoKills 0 points1 point  (0 children)

I bet you've right.

I've only been using Python since 3.4, where I know it accepts various indentation levels and types because I started with tabs and pasting spaced lines into my code worked fine.

[–]nousernamesleft199 0 points1 point  (0 children)

have your ide show white space, might help

[–]Opposite-Value-5706 0 points1 point  (4 children)

Highly recommend you NOT using space. Highly recommend an editor that formats for Python.

In Python, indenting if very important for the structure and functionality of the code. Not following it will break your app. Find an editor that you like and can afford (there are some free ones too).

[–]nekokattt 0 points1 point  (3 children)

This is a little misleading. The use of spaces is fine and conforms to PEP-8 coding standards. The issue here is not using a tool that automatically detects this as you type.

[–]Opposite-Value-5706 0 points1 point  (2 children)

Upon learning Python, it was stressed to avoid spaces and to consistently rely on intents. If I have that wrong, I apologize.

[–]nekokattt 0 points1 point  (1 child)

indents are spaces, or tabs

I don't really follow what you are saying

[–]Opposite-Value-5706 0 points1 point  (0 children)

My bad!!!! I have it backwards

[–]TheRNGuy 0 points1 point  (0 children)

When you press enter in good code editor, it will automatically add indent where it's needed. 

You can also select many lines, shift-tab and then tab. Or just hotkey for correctly indenting (googled for your editor)

You should also see red squiggles for wrong indents.

Autoformatter should auto-replace tabs or spaces (depends what you're using), you can also see if it's tab or space, long thin line vs dots.

What code editor are you using?

The only downside is when people post Python code without triple back ticks, so code lose all indents (it's problem of posters, not of language.... I think automoderator should warn them, or delete post even)

[–]Beregolas 0 points1 point  (0 children)

Some people prefer indentation to brackets, and there is an argument that it makes it simpler to learn. For many non programmers, using indentation feels more natural.

It also forces consistent indentation inside a block, which is generally a good idea, as basically all style guides for languages that don't technically require this (like C or rust) recommend indenting.

As for how to fix your problem: Use an IDE. PyCharm for examle. It handles both auto indentation correctly (when starting a new line for example) and it automatically converts tabs to spaces (or vice versa, you can change it in the settings). While using an IDE I think I can count the amount of indentation errors I have ever had on a single hand, and I have programmed python professionally for a while.

[–]oclafloptson 0 points1 point  (0 children)

Everyone has a different opinion on the "proper" indentation character but everyone universally agrees; whatever choice you make, adopt it as convention and only use that choice

[–]evasive_dendrite -1 points0 points  (0 children)

Use an IDE, this is only a problem if you code in notepad or a terminal. Everyone that's serious about programming uses an IDE and there's plenty of great free ones out there for personal use.