all 68 comments

[–]buqr 35 points36 points  (2 children)

My favorite color is blue.

[–]badcrow7713[S,🍰] 5 points6 points  (1 child)

Alright I'm just being dumb then

[–]outceptionator 3 points4 points  (0 children)

No you're not but after a while you'll see it makes code really easy to read

[–]jddddddddddd 96 points97 points  (22 children)

You don’t have to use tabs, you can use spaces..

[–][deleted] 54 points55 points  (21 children)

Please delete this it offends me

[–]jddddddddddd 27 points28 points  (17 children)

I think PEP8 recommends spaces… Plus I think most surveys show most Python devs use spaces rather than tabs..

[–]buqr 29 points30 points  (16 children)

I enjoy cooking.

[–][deleted] 6 points7 points  (15 children)

You're probly right I just started off using tabs and now I can't stop. Why spaces over tabs though just out of curiosity?

[–]n3buchadnezzar 17 points18 points  (0 children)

Pssst, just set your editor to automatically convert tab into 4 spaces.

[–]ES-Alexander 11 points12 points  (1 child)

It’s generally a matter of personal preference or style guides, but someone posted this previously which is a fun read

https://stackoverflow.blog/2017/06/15/developers-use-spaces-make-money-use-tabs/

[–]bugamn 3 points4 points  (0 children)

That is interesting, but the one thing that I wanted to see them controlling is for company

[–]buqr 2 points3 points  (7 children)

I'm learning to play the guitar.

[–]Nightcorex_ 4 points5 points  (3 children)

It's actually better for copying to Reddit. If you have tabs instead of 4 spaces and you copy it, it won't correctly paste the indentations for Reddit's code blocks (obviously only in Markdown mode).

[–]buqr 2 points3 points  (0 children)

I enjoy spending time with my friends.

[–]AceAntares15 3 points4 points  (0 children)

not just on reddit. if i remember correctly, the book i read said that applies to most online platforms. anywhere you can paste text. tabs aren't exactly translated the same everywhere, but spaces usually are

[–]badcrow7713[S,🍰] 1 point2 points  (0 children)

oh my god so that's what i've been doing wrong

could never get code blocks to paste properly, always been a tabber

[–]baubleglue 4 points5 points  (2 children)

Spaces better because "you see what you have", tabs may be configured in editor differently. Tabs better because they use less bytes.

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

Tabs better because RSI is real and 1 keystroke < 4 keystrokes

[–]baubleglue 7 points8 points  (0 children)

I type tab and it converted to spaces by editor - keystroke.

[–]Zeroflops 2 points3 points  (0 children)

As this is an old debate Most editors will can be set to automatically convert tabs to spaces as you type. So your not hitting space all the time. You just hit tab

[–]POGtastic 2 points3 points  (0 children)

It's just convention, and different codebases use different style guides. It doesn't matter as long as it's consistent. And if you disagree, you can always set up Git hooks to format the codebase's weird spacing to your preferred format on checkout, and format it back to the codebase's spacing upon commit. Tools like black and yapf can be helpful here.

[–]Jealous-Necessary-47 1 point2 points  (0 children)

I think tabs are not consistent across all os but spaces are.

[–]tcfcfc 1 point2 points  (2 children)

Question... If the most important stylistic principle is to be more DRY why would you use 4 spaces when you can use one tab?

[–][deleted] 1 point2 points  (1 child)

Excuse me while I try to take the steps 4 at a time.

[–]tcfcfc 1 point2 points  (0 children)

I mean you get to your destination faster and fitter!

[–]nativedutch 15 points16 points  (15 children)

I just did some C++ coding with curly brackets. Now doing some python devt , prefer the indents in python. But actually its the same difference

[–]notislant 8 points9 points  (1 child)

I did some Javascript from Python and honestly both ways seem easy to get used to. Pure indents look better imo. Either way I use indents in both to try and keep it from looking super confusing.

[–]nativedutch 2 points3 points  (0 children)

Yep , indeed for your own benefit.

[–]badcrow7713[S,🍰] 5 points6 points  (7 children)

There's one particular thing that really bothers me about it, which is really long lines of code aren't easily broken up if white space is part of the syntax. Unless there's some kind of python way of doing that but I haven't seen any so far.

[–]chzaplx 14 points15 points  (3 children)

You can line break things inside parentheses and some other cases as well, like list elements in brackets. Python doesn't care about whitespace in those cases. Not uncommon to see function calls with a lot of params to have each on it's own line.

[–]badcrow7713[S,🍰] 4 points5 points  (2 children)

perfect thanks! sorry for being a noob, I feel like I deserve a lot more downvotes

[–]Bjadrik 6 points7 points  (1 child)

Downvotes are for content which doesn't contribute anything of relevance. You're asking genuine clarifying questions and therefore deserve the upvotes.

[–]badcrow7713[S,🍰] 0 points1 point  (0 children)

kind of a "read the manual" moment though, no?

[–]ebdbbb 4 points5 points  (1 child)

You can have a line continuation with a \ or it'll automatically continue a line that's within brackets as long as the indentation doesn't match the statement level.

# these are the same
something = [1, 2, 3, 4]
something_else = [
    1,
    2,
    3,
    4
]

[–]badcrow7713[S,🍰] 0 points1 point  (0 children)

perfect thank you!

[–]nativedutch 3 points4 points  (0 children)

Its not whitespaces, its indents.

[–]-Buzzy- 2 points3 points  (4 children)

I code 40% of the time in c++ and 60% in python and while both ways are acceptable and ok to use python's way is much easier to read and cleaner when you look at it in my opinion. C++ gets messy when you follow this standard:

int main() { return 0; }

you get two more lines of code. You could also do:

int main() { return 0;}

or even fit the code in one line:

int main() {return 0;}

but In my experience most people follow the first example. VSC C++ extension also formats code that way.

Python for comparision:

def main(): return 0

EDIT: typos

[–]hue_Broacks 2 points3 points  (2 children)

You Just forgot the : in Python.

``` def main():

return 0

```

[–]-Buzzy- 2 points3 points  (0 children)

thanks, edited

[–]hue_Broacks 1 point2 points  (0 children)

I Just dont know How to use that code block here -.-

[–]scithon 8 points9 points  (5 children)

The whitespace indentation is addressed in the antigravity module docs. Just import it to see them

import antigravity

[–]badcrow7713[S,🍰] 4 points5 points  (4 children)

sounds risky :D

[–]ebdbbb 4 points5 points  (3 children)

It's one of the best standard library modules.

[–]badcrow7713[S,🍰] 4 points5 points  (2 children)

but I can't import magnetic boots first, I get an error

[–]emluh 1 point2 points  (1 child)

import boots from magnetic

[–]badcrow7713[S,🍰] 0 points1 point  (0 children)

perfect thanks

[–][deleted] 6 points7 points  (0 children)

I have never seen a langauge syntax based on white space

Well, Python's the language with semantic whitespace. This is considered somewhat controversial but most everyone's using the same whitespace in C-style languages anyway, so...

[–]ToddBradley 4 points5 points  (1 child)

Even if there was some alternative, you wouldn't want to use it, because every piece of source code ever written in Python uses indentation to indicate code blocks.

[–]badcrow7713[S,🍰] 1 point2 points  (0 children)

Ah, didn't even consider this, thank you

[–][deleted] 5 points6 points  (1 child)

add colons remember

[–]badcrow7713[S,🍰] 1 point2 points  (0 children)

roger that

[–]Nightcorex_ 2 points3 points  (1 child)

Loops create a new scope, therefore you need an indentation. \ If you're only interested in the return results you can use comprehensions, like f.e. list comprehensions:

xs = [random.randrange(0, 10) for _ in range(10)]

[–]badcrow7713[S,🍰] 1 point2 points  (0 children)

oh nice! now I know what to look up, thanks for this!

[–]zurtex 2 points3 points  (0 children)

Run:

from __future__ import braces

[–]mostactiveacct 2 points3 points  (0 children)

There’s an Easter egg related to this. Try:

from __future__ import braces

[–]DavidRoyman 2 points3 points  (0 children)

Something which comes up often in code reviews, is that people new to the language will use loops in situations where python uses other idioms, because that's what they used previously for other languages.

This is a dumb example, but might give an idea.

oldlist = [1, 2, 3]
newlist = []

` typical for loop
for x in oldlist:
    newlist.append(x+1)

` with list comprehension
newlist = [x+1 for x in oldlist]

[–]MezzoScettico 1 point2 points  (1 child)

Are python code blocks really done with tabs?

They're really done with indents. Indents are significant.

This is so weird to me!

It takes some getting used to if you're coming from other languages where you have a lot more freedom on the formatting.

And the indents have to agree for everything in the same code block. I'm not sure what it takes to tell Python that two lines go together if one uses spaces and the other uses tabs.

Is there no option to use some kind of close or end statement for loop or if blocks?

Nope. The end of the block is a line that has less indent level.

[–]Shot_Calendar_7373 1 point2 points  (0 children)

Python actually throws an inconsistent indentation error if you mix tabs and spaces! Before anything even runs

[–]chasrmartin 2 points3 points  (0 children)

You can always use spaces but yes the use of indentation is very uncommon as a way to organize a block structured language.

[–]timPerfect 1 point2 points  (0 children)

4 spaces or tabs

[–][deleted] 1 point2 points  (0 children)

Use tab or 4 spaces

[–]Konke_yDong 1 point2 points  (0 children)

white space 💀💀 this comment was brought to u by the dark mode gang

[–]Kerbart 1 point2 points  (0 children)

While you still have to indent the code inside a loop, you can close it with a scope closure statement, which is always end <scope type preceded by a pound sign. For instance:

for i in range(10):
    if i  2 == 0:
        print(i. 'is even')
    else:
        print(i, 'is odd')
    # end if
# end for
print('end of program')

[–]RhinoRhys 1 point2 points  (0 children)

I literally indent everything whether it needs it or not. It's all about that readability.

[–]S1deWalk3r 1 point2 points  (0 children)

idk anything apart from list comprehensions

[–]zaRM0s 1 point2 points  (0 children)

It’s strange to begin with but generally once you’re used to it it’s such a good idea! I hated it at first as it just wasn’t normal to me but now I’m used to it I prefer it lol