简体   繁体   中英

Python loop syntax

In python the for loop and while loop and even the control statements are using the indentation to highlight the block of code needed to be run. Is there anyway to bypass this? I have so much problem with indentations, as for long codes it makes the code less readable. Is there any packages to make the code more readable? Can we use curly brackets?

No, there's no alternative to correct indentation. It's a fundamental part of the language.

I have so much problem with indentations...

Perhaps you could benefit from a better editor. A good code editor or IDE will auto-indent code for you. Avoid using something like Notepad which always starts new lines at column 1. The bare minimum you need is an editor that will start new lines at the same indentation level as the previous line.

...as for long codes it makes the code less readable.

I don't see how that can be true. Bad indentation is one of the most pervasive problems I see with new coders' code. It makes code super hard to read. It's like having poor punctuation or capitalization in English. Indentation is a basic readability tool.

Can we use curly brackets?

It scares me a little that you want to forego good indentation and use curly braces instead. Even if curly braces were allowed, you should still indent your code properly. Don't you find this

if (foo) {
    while (bar != baz) {
        quux();
    }
}

easier to read than this?

  if (foo) {
while (bar != baz) {
    quux();         }
    }

And if you already indent your code as a matter of habit the curly braces are redundant. That's what led Python's designers to remove them.

if foo:
    while bar != baz:
        quux()

I am afraid not. But you can use IDE such as JetBrains which makes it easier to have proper indendation.

Can we use curly brackets?

from __future__ import braces ;)

The answer from the creators of Python is, as you can see after executing above code, "not a chance".

The question is: why do you consider your code unreadable?

With indentation (and style guidelines like PEP8), the code should be actually more readable . - Python is designed to be easily readable. The Zen of Python starts:

Beautiful is better than ugly.

Explicit is better than implicit. Simple is better than complex.

Complex is better than complicated. Flat is better than nested.

Sparse is better than dense.

Readability counts.

This means, the problem may not be Python but your coding style . Or some configuration you're using.

I can only think of, like, 2 cases where indentation would make programmer's life more difficult, and both of those are fixed with good IDEs or tools. Or hardware - bigger screen - but good code with braces uses the same number of lines or more than indented Python, so I assume it's not the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM