Table des matières
回复内容:
Maison développement back-end Tutoriel Python Python缩进的代码风格真的很好吗?

Python缩进的代码风格真的很好吗?

Jun 06, 2016 pm 04:22 PM
python twitter

Python缩进的代码风格真的很好吗?
像这种…我都不觉得python风格很好,阅读和书写都不方便,现在编辑器这么方便,分号都能自动完成。大家都觉得Python风格很好么?

(图片来源:twitter.com/thedirtycod

回复内容:

贴一段 Learning Python, 5th Edition - O'Reilly Media 好了:

Why Indentation Syntax?

The indentation rule may seem unusual at first glance to programmers accustomed to C-like languages, but it is a deliberate feature of Python, and it’s one of the main ways that Python almost forces programmers to produce uniform, regular, and readable code. It essentially means that you must line up your code vertically, in columns, ac- cording to its logical structure. The net effect is to make your code more consistent and readable (unlike much of the code written in C-like languages).


To put that more strongly, aligning your code according to its logical structure is a major part of making it readable, and thus reusable and maintainable, by yourself and others. In fact, even if you never use Python after reading this book, you should get into the habit of aligning your code for readability in any block-structured language. Python underscores the issue by making this a part of its syntax, but it’s an important thing to do in any programming language, and it has a huge impact on the usefulness of your code.

Your experience may vary, but when I was still doing development on a full-time basis, I was mostly paid to work on large old C++ programs that had been worked on by many programmers over the years. Almost invariably, each programmer had his or her own style for indenting code.

强制缩进其实是Python保持代码风格统一且可读性良好的机制。
同时并不真诚的祝愿那些代码风格写的跟狗屁一样还觉得自己又屌又机智又个性的同学将来不被合作的同事打飞。 贴的图是Java啊。。。 你个java用什么python式的缩进啊……
我用 纯正的python 改写一下你的代码:

<span class="k">class</span> <span class="nc">Permuter</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">permute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">a</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">n</span><span class="o">==</span><span class="mi">0</span><span class="p">:</span>
            <span class="k">print</span> <span class="n">a</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
                <span class="n">permute</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">swap</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">0</span> <span class="k">if</span> <span class="n">n</span><span class="o">%</span><span class="mi">2</span> <span class="k">else</span> <span class="n">i</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">swap</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">):</span>
        <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="p">],</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
Copier après la connexion
其实任何语言在编写的时候都会把缩进作为一种规范。即使缩进对于编译器没有实际意义,但是却增大了可读性。所以既然有没有用都要缩进,也就不存在Python这么做好不好的问题。

主流IDE都有自动排版功能,你试一下就知道了。补充一句,我是指其他语言的自动排版。Python的自动排版不能处理语句开始的自动缩进。只能改一下字符间的距离。 python这种东西就是强制叫你缩进规范化。如果你遵循规范,python这种东西直接省掉了大括号分号,没有其他习惯上的改变。如果你压根无视缩进规范…抱歉我真不想看没缩进的代码,而且我相信我不是一个人。

至于说写的时候不缩进写完再自动排缩进什么的…写个复杂点的东西估计查错都要抓狂吧…


话说贴个java图已经被吐槽很多了可还是忍不住吐槽…你好歹贴个语法像一点儿的啊… 你…贴…个…java…干…嘛… 缩进解决了花括号要不要换行写的问题。
现在编辑器这么方便,分号都能自动完成。
连分号都不想写,那还不用Python?直接不写嘛。:-D 不是很好 大神们已经用也许“蹩脚”的语言披荆斩棘,你tm还在讨论这些?? 反正都要换行缩进加括号,
少按一次键盘,并且自动对齐不是很好么
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Quels sont les outils d'analyse des données de la blockchain? Quels sont les outils d'analyse des données de la blockchain? Feb 21, 2025 pm 10:24 PM

Quels sont les outils d'analyse des données de la blockchain?

Comment télécharger Deepseek Xiaomi Comment télécharger Deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

Comment télécharger Deepseek Xiaomi

Comment lui demandez-vous Deepseek Comment lui demandez-vous Deepseek Feb 19, 2025 pm 04:42 PM

Comment lui demandez-vous Deepseek

Comment rechercher Deepseek Comment rechercher Deepseek Feb 19, 2025 pm 05:18 PM

Comment rechercher Deepseek

Comment programmer Deepseek Comment programmer Deepseek Feb 19, 2025 pm 05:36 PM

Comment programmer Deepseek

Où est l'entrée officielle de Deepseek? Dernier guide de visite en 2025 Où est l'entrée officielle de Deepseek? Dernier guide de visite en 2025 Feb 19, 2025 pm 05:03 PM

Où est l'entrée officielle de Deepseek? Dernier guide de visite en 2025

Bitget Exchange Official Site Web Login Dernière entrée Bitget Exchange Official Site Web Login Dernière entrée Feb 18, 2025 pm 02:54 PM

Bitget Exchange Official Site Web Login Dernière entrée

Comment utiliser Deepseek pour régler les comptes Comment utiliser Deepseek pour régler les comptes Feb 19, 2025 pm 04:36 PM

Comment utiliser Deepseek pour régler les comptes

See all articles