缩进代码可读性 [英] Indentation for code readability

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

问题描述

您好,


这是我在C ++中所做的,现在不能在python中使用:


pushMatrix()

{

drawStuff();


pushMatrix();

{

drawSomeOtherStuff()

}

popMatrix();

}

popMatrix();


大括号没有任何功能意义,但显着增加了

的可读性。我希望能够在

python中做同样的事情。由于大括号不可用并且在没有

的情况下缩进if if或while条件不起作用,我开始质疑是否

这在python中是可能的。


有什么想法吗?


MDE

Hello,

Here is what I do in C++ and can not right now in python :

pushMatrix()
{
drawStuff();

pushMatrix();
{
drawSomeOtherStuff()
}
popMatrix();
}
popMatrix();

The curly brackets have no functional meaning but increase the
readability significantly. I want to be able to do the same thing in
python. Since curly brackets are not available and indenting without
an if or while conditional doesn''t work, I have started to question if
this is possible in python at all.

Any ideas ?

MDE

推荐答案

DE schrieb:
DE schrieb:

你好,


这是我在C ++中做的,现在不能在python中:


pushMatrix()

{

drawStuff();


pushMatrix ();

{

drawSomeOtherStuff()

}

popMatrix();

}

popMatrix();


大括号没有任何功能意义,但显着提高了

的可读性。我希望能够在

python中做同样的事情。由于大括号不可用并且在没有

的情况下缩进if if或while条件不起作用,我开始质疑是否

这在python中是可能的。


有什么想法吗?
Hello,

Here is what I do in C++ and can not right now in python :

pushMatrix()
{
drawStuff();

pushMatrix();
{
drawSomeOtherStuff()
}
popMatrix();
}
popMatrix();

The curly brackets have no functional meaning but increase the
readability significantly. I want to be able to do the same thing in
python. Since curly brackets are not available and indenting without
an if or while conditional doesn''t work, I have started to question if
this is possible in python at all.

Any ideas ?



我现在已经考虑了几分钟了,我怀疑它会增加可读性。
也许是因为你发明了

风格而不是其他风格。

格式化C代码有一些标准,甚至这几个都有

引起开发人员之间的许多讨论。

Python有一个约定(在PEP 8中定义),你越深入,你就会越喜欢这个语言。

BTW:有一种方法可以做到这一点是Python'

哲学的主要思想之一。


Thomas

I''ve been thinking about that for some minutes now and I have doubts
that it will increase the readability. Maybe for you as you invented
that style but not for others.
There are a few standards for formatting C code and even this few have
cause many discussions between developers.
Python has one convention (defined in PEP 8) and the deeper you dive
into the language the more you will like it.
BTW: having one way to do it is one of the main ideas of Python''s
philosophy.

Thomas


3月30日下午7:04,DE < devrim.er ... @ gmail.comwrote:
On Mar 30, 7:04 pm, "DE" <devrim.er...@gmail.comwrote:

您好,


这是我在C ++中所做的并且现在不能在python中:


pushMatrix()

{

drawStuff();


pushMatrix();

{

drawSomeOtherStuff()

}

popMatrix() ;}

popMatrix();


大括号没有任何功能意义,但显着提高了

的可读性。我希望能够在

python中做同样的事情。由于大括号不可用并且在没有

的情况下缩进if if或while条件不起作用,我开始质疑是否

这在python中是可能的。


任何想法?
Hello,

Here is what I do in C++ and can not right now in python :

pushMatrix()
{
drawStuff();

pushMatrix();
{
drawSomeOtherStuff()
}
popMatrix();}

popMatrix();

The curly brackets have no functional meaning but increase the
readability significantly. I want to be able to do the same thing in
python. Since curly brackets are not available and indenting without
an if or while conditional doesn''t work, I have started to question if
this is possible in python at all.

Any ideas ?



你*可以*使用圆括号和/或方括号。例如


def pushMatrix():

drawStuff()

pushMatrix()



drawSomeOtherStuff()



[

drawEvenMoreStuff()

]

popMatrix()


无论你*应该*那是一个不同的问题......它有点像

就像一个一个苏格兰绅士的英文定义:一个可以玩风笛的人,但是没有:-)


HTH,

John

You *can* use round brackets and/or square brackets. E.g.

def pushMatrix():
drawStuff()
pushMatrix()
(
drawSomeOtherStuff()
)
[
drawEvenMoreStuff()
]
popMatrix()

Whether you *should* do that is a different question ... It''s a bit
like an English definition of a Scottish gentleman: one who can play
the bagpipes, but doesn''t :-)

HTH,
John


DE写道:
DE wrote:

您好,


这是我在C ++中所做的,现在不能在python中使用:


pushMatrix()

{

drawStuff();


pushMatrix();

{

drawSomeOtherStuff()

}

popMatrix();

}

popMatrix();


大括号有没有功能意义但增加

可读性显着。我希望能够在

python中做同样的事情。由于大括号不可用并且在没有

的情况下缩进if if或while条件不起作用,我开始质疑是否

这在python中是可能的。


有什么想法吗?
Hello,

Here is what I do in C++ and can not right now in python :

pushMatrix()
{
drawStuff();

pushMatrix();
{
drawSomeOtherStuff()
}
popMatrix();
}
popMatrix();

The curly brackets have no functional meaning but increase the
readability significantly. I want to be able to do the same thing in
python. Since curly brackets are not available and indenting without
an if or while conditional doesn''t work, I have started to question if
this is possible in python at all.

Any ideas ?



如果为True,你可以使用




#do stuff


但是我对这种自我造成的噪音没有同情。


使用Python 2.5你可以做得更好 - 你可以模仿应该有的东西

首先是你的C ++示例中的RAII:


来自__future__ import with_statement


来自contextlib import contextmanager


def push_matrix():

print" push"

def pop_matrix():

print" pop"

@contextmanager

def matrix():

m = push_matrix()
试试:

收益率m

终于:

pop_matrix()

with matrix():

print" do stuff"

with matrix():

print" do more stuff"


彼得

You could use

if True:
# do stuff

but I have no sympathy for such self-inflicted noise.

With Python 2.5 you can do even better -- you can emulate what should have
been RAII in your C++ example in the first place:

from __future__ import with_statement

from contextlib import contextmanager

def push_matrix():
print "push"

def pop_matrix():
print "pop"

@contextmanager
def matrix():
m = push_matrix()
try:
yield m
finally:
pop_matrix()

with matrix():
print "do stuff"
with matrix():
print "do more stuff"

Peter


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

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