简体   繁体   English

在 Python 块中开始和结束

[英]Begin and End in Python blocks

I am using Python我正在使用 Python

but the space gap is making my life very hard with it但是空间差距让我的生活变得非常艰难

example例子

when I use the if statement当我使用 if 语句时

if Parm2 == 1:
    Ch = "A"
elif Parm2 == 2:
    Ch = "B"
elif Parm2 == 3:
    Ch = "C"
else:
    continue
mdl = CallFunc(Parm2)

print("XX Always Print XX")

now the "XX Always Print XX" should be printed regardless现在无论如何都应该打印“XX Always Print XX”

but due to my mistake it is inside the if statement which cause me long time to find但由于我的错误,它在 if 语句中导致我很长时间才找到

the actual if statement is nested and longer实际的 if 语句是嵌套的并且更长

I wonder if there is a method I can use begin/end or {} in such statements in Python我想知道是否有一种方法可以在 Python 的此类语句中使用 begin/end 或 {}

something like就像是

UPDATE更新

for the people who focus on the IF statement对于那些关注 IF 声明的人

if Parm2 == 1:
{
    Ch = "A"
}
elif Parm2 == 2:
{
    Ch = "B"
}
elif Parm2 == 3:
{
    Ch = "C"
}
else:
{
    mdl = CallFunc(Parm2)
}
print("XX Always Print XX")

Happy now??现在开心??

ok now how to get the brackets work in Python?好的,现在如何让括号在 Python 中工作?

Python is indentation based. Python 是基于缩进的。 Yeah, its harder to read and make mistakes like you indicated, but that's what it is.是的,像你所说的那样更难阅读和犯错误,但就是这样。

Think about downloading an IDE for python, like Pycharm , they are helpful for identifying errors like this one, they also have an "auto-indent" feature.考虑为 python 下载 IDE,例如Pycharm ,它们有助于识别此类错误,它们还具有“自动缩进”功能。 But no, Python is indentation based.但是不,Python 是基于缩进的。

What the Python designers realized is that braces or begin/end keywords are mostly noise to human programmers. Python 的设计者意识到大括号或begin/end关键字对人类程序员来说大多是噪音。 They generally recognize the structure of code by its layout.他们通常通过布局来识别代码的结构。 For instance, if you were to write the C code:例如,如果您要编写 C 代码:

if (condition)
    x = y;
    w = z;

a human would often not notice that the braces are missing, and assume that both assignments are controlled by the condition.人们通常不会注意到大括号丢失,并假设两个分配都受条件控制。 Writing code like this is a common error, especially when you start with a block that has just one statement (so the braces are optional and were omitted), and forget to add braces when a second statement is added.编写这样的代码是一个常见错误,尤其是当您从一个只有一个语句的块开始时(因此大括号是可选的并且被省略),并且在添加第二个语句时忘记添加大括号。 (See Why is it considered a bad practice to omit curly braces? ). (请参阅为什么省略花括号被认为是一种不好的做法? )。

Conversely, if you write相反,如果你写

if (condition) {
    x = y;
w = z;
}

it looks like w = z;它看起来像w = z; is not part of the conditional.不是条件的一部分。

Braces mainly exist for the benefit of software that processes code (compilers, editors, IDEs), they make it easier for them to detect groups of code.大括号的存在主要是为了处理代码的软件(编译器、编辑器、IDE),它们使它们更容易检测代码组。 The Python designers decided to mirror the way humans read code in their parser, rather than forcing humans to adapt to the computer's needs. Python 设计者决定模仿人类在解析器中读取代码的方式,而不是强迫人类适应计算机的需求。

Braces allow for more flexible code layout, but in practice it's usually condiered wrong to take advantage of this.大括号允许更灵活的代码布局,但在实践中,利用这一点通常是错误的。 Writing code like像这样写代码

while (something) { statement1; statement2; 
statement3; }

is less readable than可读性低于

while something:
    statement1
    statement2
    statement3

Python does allow some flexibility: You can separate statements on the same line with ; Python 确实具有一定的灵活性:您可以使用;分隔同一行中的语句。 , and put the contents of a conditional on the same line after the : . , 并将条件的内容放在:之后的同一行。 But writing like this is not considered Pythonic, and should be used only in very special circumstances ( this blog post describes those cases).但是这样的写法不被认为是 Pythonic,只应在非常特殊的情况下使用( 这篇博文描述了这些情况)。

There's always some adjustment necessary when you're learning a new programming language and you're accustomed to the patterns of the languages you previously used (many programmers have refused to learn Lisp, because of its L ots of I rritating, S tupid P arentheses).当你学习一门新的编程语言并且你已经习惯了你以前使用的语言的模式时,总是需要进行一些调整(许多程序员拒绝学习 Lisp,因为它有很多令人讨厌的、愚蠢括号)。 But give it a little time and you'll get used to it.但是给它一点时间,你会习惯的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "在 Python 中,如何提取以相同模式开头但没有不同结尾的多个文本块?" - In Python, how do I extract multiple blocks of text that begin with same pattern, but no distinct end? Python SQLite-如何手动开始和结束事务? - Python SQLite - How to manually BEGIN and END transactions? python正则表达式获得多行开头和结尾 - python regex getting a multiple lines that begin and end with 搜索开始字符串和搜索结束字符串,然后在python中打印开始到结束行之间的所有行 - search begin string and search end string then print all lines between begin to end lines in python 如何获取 LaTeX 文件中的所有 `\\begin{definition}...\\end{definition}` 块? - How can I get all the `\begin{definition}...\end{definition}` blocks in a LaTeX file? Python相当于自定义类的C ++ begin()和end() - Python equivalent of C++ begin() and end() for custom classes 如何计算每个开始和结束的差异 - Pandas Python - How calculing the difference for each begin and end - Pandas Python 在Python中是否可以修饰函数,以便记录其开始和结束? - Is it possible in Python to decorate a function so that its begin and end are logged? Python:CircularQueue 从队列的开始到结束打印项目 - Python: CircularQueue print items from begin to end of Queue Python SQLite3-是否在每个插入语句上开始,结束事务? - Python SQLite3 - Does it BEGIN, END transaction on each insert statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM