+ 30
Why does Python consider white space to be code?
IMHO white space should be there for programmers to make their code easier to read and a language shouldn't care if TABs or spaces are used. If a coder used TABs, another coder working on the same project should be able to use the same code in an editor, map the TAB characters to any number of spaces to suit his/her readability and work on the same code. But Python doesn't seem to allow this freedom. To me this is annoying. To those who get offended easily, this part of the question is opinion. What do you think?
37 Antworten
+ 18
Python is not the only language that cares about whitespace by the way! \n is very important in javascript for example and even in C++ there are cases where spaces make a difference.
I'm more of a semicolon person myself but I usually recommend python to newcomers when asked, because it forces them to write readable code.
I think it's an okay design decision. The spaces/tabs thing is not an issue as you don't need to indent with spaces but you can use a tab (\t) instead, and you can map that to however many spaces you want in your editor. (If you use anything other than 4 though we can never be friends, sorry!)
+ 16
Every language may have annoying parts of its own.
In languages like C you have to plaster everything with curlies and semicolons. If one of them is missing somewhere or misplaced, things quickly go South.
Especially annoying when there is no exception but the program just does weird stuff. Then the big search begins: Where, where?
Even worse when the indentation is 'original' (over the place) so you don't even have visual cues of what the logic of the code should be.
Python solves both issues with one stroke: It gets rid of the required semicolons and curlies altogether and makes a good indentation - which should be our goal anyway! - our one single duty.
And, oh how sweet a duty it is, how absolutely easy it makes everything!
Also consider that you can do with white space almost whatever you want; only the newlines and the indentation is forced. And I think the tabs-vs-space issue is solvable.
+ 16
HonFu you have a point there. I was unaware that you have freedom with whitespace in Python apart from indentation. I might give Python a go and see how I fare. I am not necessarily a fan of any language but just giving each one a go in turn, simply because the basics of each are easy to learn here on Sololearn. I didn't know anything about Ruby either (apart from the name) until about a month ago. Ruby seemed a bit intuitive to me for someone new to the language but maybe if I try a bit more Python coding I might eventually start to like it. I basically learn on Sololearn by trying brief codes and asking questions. One day I will get my balance right 😊.
+ 14
Janning⭐ lol. Schindlabua I do usually configure my tabs to be 4 spaces, so I think we could be friends 😊.
+ 13
Kishalaya Saha I'm a total beginner to Python, whereas I have learnt C/C++ in the past. So, my question has a newbie flavor to it. Perhaps I should have rephrased that part of the question better. What I meant was that you don't get the freedom to indent a line of code at will in Python be it with tabs or spaces. But through this question I am learning that people with Python expertise are quite religious about 'right indentation' which is enforced by the language.
+ 13
Thanks HonFu for that helpful demo! 👍
+ 12
Janning⭐ I must confess, I like a good debate 😀.
+ 8
NULL, you can write a long line with Python, too, you just have to put a \ before hitting enter. While you are in any set of parentheses you don't even need that.
You can also put many commands in a line separated with semicolons if you really want to.
Maybe I like it because I started with Python and so it feels like the 'normal' thing to me.
I get uncomfortable when I look at Ruby code, though: Wait, no indents, no curlies, no semicolons, not even function call parentheses!? :-D
+ 6
I feel like we all just became friends 😀
+ 5
I believe the whitespace is a main feature of the lexical. Its design and usage help maintain hierarchy and structure. Some Python "Standards" are not strictly enforced. Therefore, the community develops socially acceptable standards, formatting code is not an exception. 1 Tab should equal 4 whitespace. If a developer decides to use another format, with regards to whitespace, I feel that developer needs to provide a docstring or comments explaining the formatting. However, several great IDEs will catch the 'strange' formatting, it is up to the developer to fix the formatting.
+ 5
Janning⭐ I think being able to configure the tab stops on Sololearn would be a good feature!
+ 5
Kishalaya Saha You can't mix tabs and spaces (I think, in the same indentation block) because that would be horrible:
https://code.sololearn.com/cNipu7rJdO8n/?ref=app
Tabs are fine in some corporate policies; the recommendation is basically: "be consistent".
+ 5
Hi, I could get along with tabs and spaces being used for indentation defining compound statements. The problem I have is when I try and use code segments from several different programs I've created. Some use spaces and the other has tabs it is very difficult to get the indentation correct. The really bad one case I found is useing a single space followed by a tab that is a two level of indentation even though it only looks like a one level on the screen.
+ 5
Sonic, I have made a little demonstration.
https://code.sololearn.com/c9a0cG9dpVUr/?ref=app
+ 5
Because to them nothing is a waste🤣
+ 4
Now you gone an' dunnit, Sonic: You've started the tabs-vs-spaces debate. 🤪
+ 4
lol. I don't mind the extra punctuation, but if I had to, I guess I'd cast my lot with spaces over tabs... then turn right around and use tabs everywhere like a freaking hypocrite. 😁
+ 4
Dear SoloLearn,
Please make the default tab stops in the Code Playground 2 spaces instead of 4 so we can read better on our mobile devices.
Best,
-- Community Troll
PS: just kidding
+ 4
Kishalaya Saha in Ruby code blocks are delimited by the keyword 'end', which, one might argue, is a curly in disguise. ;-)
Sonic, it would be a bit silly to be seriously 'passionate' or 'religious' about indentation as a pythonist - because it is just enforced. Fans of C probably don't get religious about curlies either (except about where exactly they have to sit ^^).
My point: You get a lot of whitespace freedom in Python. All you really have to do watch out for is that the beginnings of the lines of a block are aligned, and I don't see where this is harder or more restrictive than having to write a lot of curlies.