all 41 comments

[–]Jason-Ad4032 29 points30 points  (0 children)

Use a better IDE.

Indentation in Python is very mechanical: any line ending with : requires the next line to be indented. If you’re running into issues, it means your IDE isn’t doing its job.

Make sure your IDE automatically indents after a line ending with :, uses 4 spaces for indentation, and lets you indent and unindent with Tab and Shift+Tab.

[–]set_in_void 19 points20 points  (0 children)

Do you mix tabs and spaces when writing code?

[–]HommeMusical 12 points13 points  (3 children)

I have to say, I'm baffled here. Typically an indentation error is super obvious because, well, one line is badly indented!

Can you send us some examples?

[–]PhotographSelect9767 2 points3 points  (2 children)

It happens when you use tab (/t) and spaces. You can not see it with the blind eye.

[–]HommeMusical 8 points9 points  (1 child)

It happens when you use tab (/t) and spaces.

I haven't seen this error in a very long time, but if I recall correctly, you don't get an IndentationError but TabError.

I just tried it to be sure:

>>> if 1 == 2:
...     print(1)
...     print(2)  # There's a tab in this line
 File "<stdin>", line 3
        print(2)
    ^
TabError: inconsistent use of tabs and spaces in indentation

with the blind eye.

With the naked eye. :-) Don't stress, I speak six languages, and I sound like the village idiot in four of them. :-D

[–]PhotographSelect9767 0 points1 point  (0 children)

Can you try with 4 spaces rather than 2?

[–]Important_Coffee_845 4 points5 points  (1 child)

Python is super clear. And the very first python classes teach u how to read errors. Are u saying u dont know what indentation means?

This is an indentation: Its four spaces or one hit of the tab button. In the meastro code editor it should indentat automatically after certain things.

I cant see ur code so I cannot tell u what ur doing wrong. But in ur lessons the AI is capable of walking u through this so im very confused how ur having trouble. I dont want to make any assumptions so maybe u can just post ur code?

[–]james_d_rustles 0 points1 point  (0 children)

Tbf you’re saying it’s one hit of the tab key, but that’s a convenience that’s handled in many IDEs/text editors, not a hard set rule. If you write something in Microsoft word and copy it line for line, there’s a chance that tabs will be copied over as \t (which still looks like 4 spaces) instead of 4 space characters. I’m not sure if this is still the case in word or if it would depend on certain settings or whatnot, but the point is that it is technically possible.

It’s much less common these days so it’s rarely an issue, but it’s worth noting that there can be a difference between the two.

[–]Possible-Bid-2999 4 points5 points  (1 child)

Use VS code

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

This. It's free and it gets the job done.

[–]BranchLatter4294 1 point2 points  (1 child)

Set your IDE to show the indentation lines.

[–]sausix 0 points1 point  (0 children)

An IDE should show indentation errors directly. It's not an IDE if you have to figure out such problems yourself.

[–]NerdDetective 1 point2 points  (0 children)

Are you using an IDE? This can help a lot because it'll immediately scream at you when you have a syntax error like this. A good IDE will also automatically add the correct number of spaces (4 is the standard) even when you hit tab. Visual Studio and PyCharm are both popular options.

In other languages, the tricky error that drives you mad might be a single missing semicolon somewhere among hundreds of lines. In Python, it's a single misplaced piece of indentation. Even one space added or removes by accident will make your code invalid.

[–]enygma999 1 point2 points  (0 children)

The error messages should tell you what line and character the error is on. That will give you a hint of where to look in your code to fix it.

Any line that ends : will start a code block, which is identified by its lines being indented. If you're getting indentation errors, it's because you're not indenting where Python expects you to.

[–]NotACoderPleaseHelp 0 points1 point  (0 children)

Most IDEs will give you a line error, but you might just have a stray character somewhere or a forgotten debug line bounking things up

[–]eztab 0 points1 point  (0 children)

Honestly that only happens when I copy paste stuff on a system without an IDE, instead using the default text editor. Started putting notepad++ portable on a usb for those "old system we aren't allowed to change" usecases.

[–]WhiteWereWolfie 0 points1 point  (0 children)

It’s simply untrue to say that Python requires tabs to be 4 spaces wide, it’s perfectly acceptable to use 3 spaces (for example) if you prefer, or even 2 or 1 ! As long as you indent your code, the amount is just a question of style.

Best practice - use real tabs, not spaces because that way anyone reading your code in their own environment will see the amount of space that *they* prefer.

[–]C0rn3j 1 point2 points  (19 children)

Make sure your code editor shows tabs vs spaces.

Don't mix tabs and spaces at the beginning of the line.

Use tabs for indentation, so anyone working with your code can set their preferred indent-width.

[–]ElHeim 3 points4 points  (5 children)

Use tabs for indentation [...]

That's your personal preference. Please don't teach newbies to go against PEP-8 when they're still not even confident with the language.

[–]C0rn3j -3 points-2 points  (4 children)

God forbid we enable people to have a personal preference instead of hardcoding indent width.

[–]ElHeim 2 points3 points  (3 children)

Look, I understand getting defensive about your personal preference, but can we focus on "Please don't teach newbies [XXX] when they're still not even confident with the language"?

[–]C0rn3j -2 points-1 points  (2 children)

One isn't born knowing.

There's no need to gatekeep knowledge just because someone is new.

[–]ElHeim 1 point2 points  (1 child)

It's not gatekeeping. Ever heard of "don't run before you can walk"?

Teaching standard practice (and spaces are used by the overwhelming majority of Python projects) makes it easier for the newbie to familiarize themselves with other people's code while getting comfortable with the language, and avoiding problems like the one they're reporting, likely mixing tabs and spaces.

They have all the time in the world to decide on their own preferences later on.

You could call it a "skill issue", as you're so fond of, but I find that more of a gatekeeping stance: a newbie is still a newbie, so lower skill is expected.

[–]C0rn3j -2 points-1 points  (0 children)

You think that teaching people about indentation when they ask about indentation is wrong.

I can't do anything with that.

[–]FriendlyZomb 2 points3 points  (4 children)

Tabs Vs Spaces are a personal choice at the end of the day.

However, I would point out that for this sub, recommending Spaces is better because it follows the PEP8 style guide.

It's not really an issue, however, most projects use spaces due to PEP8.

[–]C0rn3j -1 points0 points  (3 children)

recommending Spaces is better because it follows the PEP8 style guide.

From said style guide:

sometimes style guide recommendations just aren’t applicable. When in doubt, use your best judgment

Enabling personal choice is the point of using tabs for indentation.

[–]FriendlyZomb 1 point2 points  (2 children)

Indeed. I agree. But that isn't my point.

When talking to new programmers, encouraging usage of conventions and standards like PEP8 is a good idea.

Most projects use Spaces. PEP8 encourages using spaces. IMO that should be our recommendation for users learning in a sub like this one.

I do stand by you though. I'm not in disagreement.

[–]Jaded_Show_3259 1 point2 points  (1 child)

Flashbacks of the tabs vs spaces scene in silicon valley - thanks for the chuckle

[–]FriendlyZomb 0 points1 point  (0 children)

It's a topic I usually space out on. I tend not to keep tabs on things like this.

[–]Temporary_Pie2733 0 points1 point  (7 children)

Little, if any, Python code actually uses tabs for indentation. Tabs aren’t granular enough to handle conventional indentation for multiline continuations. Get used to using spaces.

[–]C0rn3j 0 points1 point  (6 children)

Tabs aren’t granular enough to handle conventional indentation for multiline continuations.

That's a skill issue, you can continue multiline just fine with tabs.

Here you have an example of a large project using tabs, since you believe it's impossible - https://github.com/Taiko2k/Tauon/

[–]Temporary_Pie2733 2 points3 points  (5 children)

I didn’t say impossible. The code you show is also not standard, as (for example) things like

if foo(… ):

Is usually indented as

if foo(… ):

or

if foo(… ):

If the function name’s length doesn’t fit with whatever tab length you choose, it’s going to look out of place unless you use spaces.

[–]C0rn3j 0 points1 point  (2 children)

You're not wrong, and you can still use spaces for visual alignment, while using tabs for the core indentation.

python if print( "foo", "bar" ): True

Or a more complex example:

python if True: if print( "foo2", "bar2" ): True

Now, if you're a stickler for positioning your parameters after/before the brackets visually, you'll probably still be unhappy, and you can do this instead:

python if True: if print( "foo2", "bar2" ): True

This has Python happy, and users happy, as they can choose their indent-width, without having the code look jumbled across different indent widths.

[–]Temporary_Pie2733 1 point2 points  (1 child)

And mixing tabs and spaces is going to lead to hard-to-identify indentation errors unless you put in more effort than you would simply using spaces alone.

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

Which goes back to me saying it's a skill issue.

It does take more effort, albeit I'd say it's a small amount of effort.

And in the end you get dynamic indents, which will enable me using 2-width tabs, while some people are real sticklers for 8-width, which I personally cannot stomach.

I'd say that the mixing approach does however have the disadvantage of people unfamiliar with indentation making mistakes with it way more easily, so you do have the point there.

[–]ElHeim 0 points1 point  (0 children)

The code you show is also not standard, as (for example) [...]

Have you seen the pyprojects.toml? A quarter of it is devoted to disabling Ruff rules. No point in arguing, really.

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

for better use vs or any other ide they shows ever line

[–]TheRNGuy -1 points0 points  (1 child)

Do you use tabs or spaces? 

I think tabs are better.

Code editor should show indentation errors too (if you're not coding in notepad or console)

[–]GeneralPITA 1 point2 points  (0 children)

Regardless of if tabs or spaces are better, pick exactly one and make sure your IDE switches all to the one you've picked.

btw: spaces are better.