Python缩进 [英] Python indentation

查看:103
本文介绍了Python缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是Python的初学者,我想知道Python中的缩进是什么意思

,没有它,python脚本就无法正常工作。

为什么压痕不能那么严格,以便为

用户提供更好的自由?

这背后是否有任何合理的理由?


干杯!

Sateesh

Hi,
I am a beginner in Python, and am wondering what is it about the indentation
in Python, without which python scripts do not work properly.
Why can''t the indentation not so strict so as to give better freedom to the
user?
Is there any plausible reason behind this?

Cheers!
Sateesh

推荐答案

Sateesh写道:
Sateesh wrote:

我是Python的初学者,我想知道Python中的缩进是什么,没有python脚本无法正常工作。
为什么可以缩进不是那么严格,以便为
用户提供更好的自由吗?
这背后有什么合理的理由吗?
Hi,
I am a beginner in Python, and am wondering what is it about the indentation
in Python, without which python scripts do not work properly.
Why can''t the indentation not so strict so as to give better freedom to the
user?
Is there any plausible reason behind this?




是。它是关于可读性的。


在许多语言中,例如C或Perl,你可以编写可读的代码,但是你可以挤压你的代码在尽可能少的行中,导致难以读取,难以维护,难以调试代码[1]。每当我将一个Perl脚本翻译成Python时,我最终会得到大约25-40%的更多

行,但是这个脚本比Perl对象更具可读性。 />
(当然,主要是因为缺少



Yes. It''s about readability.

In many languages, such as C or Perl, you can write readable code, but
you can also squeeze your code in as few lines as possible, resulting in
hard to read, hard to maintain, hard to debug code[1]. Whenever I
translate a Perl script into Python, I end up with about 25-40% more
lines, but the script is much more readable than the Perl counterpart
(mostly, of course, because of the lacking


;)。


此外,大多数语言都没有不要强制缩进并使用{}或

开头语法。在Python中,你必须正确缩进你的代码,

这导致结构清晰。


我知道当你面对Python时的感受''缩进

系统起初:哦,天哪,语言正在使用重要的

空格。由于我们大多数人都来自C背景,一种绝对的自由形式语言,这似乎是一种不可接受的限制,但是当你编写代码时,它自然流动,好像你还没有写过其他的东西。而且,你不必写结束。 :)


Reinhold


[1]这并不意味着你不能写出不可理解的代码

Python ...... *笑嘻嘻*


-

Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows

mitbr?chte ,w?re das bedauerlich。 bei Windows der Umfang eines

" kompletten Betriebssystems" ist,nennt man bei Linux eine Rescuedisk。

- David Kastrup在de.comp.os.unix.linux.misc
''s ;).

Furthermore, most languages don''t force indentation and use { } or
begin-end syntax. In Python, you have to indent your code properly,
which leads to clear structure.

I know how you feel when you are confronted with Python''s indentation
system at first: Oh my god, the language is using significant
whitespace. As most of us come from a C background, an absolute
free-form language, this may seem like an unacceptable restraint, but as
you write code, it flows naturally, as if you ever hadn''t written
something else. And, you don''t have to write "end" :)

Reinhold

[1] This doesn''t mean that you cannot write ununderstandable code in
Python... *grin*

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbr?chte, w?re das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc


Sateesh写道:
Sateesh wrote:
为什么缩进不是那么严格,以便为
用户提供更好的自由?
Why can''t the indentation not so strict so as to give better freedom to the
user?




可以你指定Python拒绝你的理想构造?或者你想要抽象地谈论自由吗?


干杯,

Brian



Can you specify what desirable construction Python is denying you? Or
are you talking about freedom in the abstract?

Cheers,
Brian


这篇关于Python缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆