向现有代码添加循环语句时的 Python 缩进
在Python中,当你写了100行代码却忘记在某处添加一堆循环语句时,你会怎么做?
我的意思是,如果您在某处添加 while 语句,您现在必须缩进其下面的所有行。这并不是说你只要戴上牙套就可以完成它。转到每一行并添加制表符/空格。如果您向现有代码添加嵌套循环/if/then 语句会怎样?
我错过了一些捷径吗?
In Python, what do you do when you write 100 lines of code and forget to add a bunch of loop statements somewhere?
I mean, if you add a while statement somewhere, you've to now indent all the lines below it. It's not like you can just put braces and be done with it. Go to every single line and add tabs/spaces. What if you were adding nested loops/if/then statements to existing code?
Am I missing some shortcut?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为每个严肃的编辑器或 IDE 都支持选择多行并按 Tab 缩进或 Shift-Tab 取消所有行缩进的选项。
I think every serious editor or IDE supports the option to select multiple lines and press tab to indent or Shift-Tab to unindent all that lines.
在IDLE(标准Python IDE)中,选择代码,继续“格式”,您可以选择缩进区域、缩进区域等
in IDLE, the standard python IDE, select the code, go on 'format' and you can chooose indent region, dedent region and so on
您必须使用编辑器命令来重新缩进。
请记住:美丽胜于丑陋。
...以及《Python 之禅》的其余部分,作者:Tim Peters
You have to use an editor command to re-indent.
Keep in mind: Beautiful is better than ugly.
... and the rest of "The Zen of Python, by Tim Peters"
编辑:重写以适应
fileinput
的“怪癖”*好吧,或者
>}
。*:我最初使用 stdout.writelines 和一些生成器表达式的漂亮、简洁的组合来编写此代码。不幸的是,该代码不起作用。
fileinput.input()
返回的迭代器在您调用其next
方法之前实际上并不打开文件。它同时在 sys.stdout 上发挥其粗略的输出重定向魔法。这意味着,如果您调用 sys.stdout.writelines 并将其传递给 fileinput.input 迭代器,则您的调用和输出将转到原始标准输出,而不是由fileinput
重新映射到“当前”正在处理的文件。因此,您最终会得到应该替换文件内容的行,而只是打印到终端。可以通过在调用 stdout.writelines 之前调用 fileinput 迭代器上的 next 来解决此问题,但这会导致其他问题:到达输入文件的末尾会导致其句柄在
file.writelines
内调用时从迭代器的next
方法关闭。在 Python 2.6 下,此段错误是因为在获取下一个值后,没有进行检查(在实现 writelines 的 C 代码中)来查看文件是否仍然打开,并且文件句柄非零。迭代器。我认为在 2.7 下它只会抛出一个异常,所以这个策略可能在那里起作用。上面的代码实际上测试正确。
edit: rewrote to accomodate
fileinput
's "eccentricities"*Well, either that or
>}
.*: I originally wrote this using a nice, concise combination of
stdout.writelines
and some generator expressions. Unfortunately, that code didn't work. The iterator returned byfileinput.input()
doesn't actually open a file until you call itsnext
method. It works its sketchy output-redirection magic onsys.stdout
at the same time. This means that if you callsys.stdout.writelines
and pass it thefileinput.input
iterator, your call, and the output, goes to the original standard out rather than the one remapped byfileinput
to the file "currently" being processed. So you end up with the lines that are supposed to replace the contents of the file being instead just printed to the terminal.It's possible to work around this issue by calling
next
on the fileinput iterator before callingstdout.writelines
, but this causes other problems: reaching the end of the input file causes its handle to be closed from the iterator'snext
method when called withinfile.writelines
. Under Python 2.6, this segfaults because there's no check made (in the C code which implementswritelines
) to see if the file is still open, and the file handle non-zero, after getting the next value from the iterator. I think under 2.7 it just throws an exception, so this strategy might work there.The above code actually does test correctly.
还选择 apple-]
选择然后 Mx 'indent-region'
在 bpython 中如此简单,你必须
努力打破它
这通常是我需要知道的。另外,是的,在一个凹进去的块的上方或下方打一个支架很容易,但你知道一周后当你一天没有盯着它看时,它只会让你感到困惑。非常感谢你们。
also select then apple-]
select then M-x 'indent-region'
so easy in bpython, you'd have to
work to break it
that's generally all I need to know. also yeah it's easy to slap a brace above or below a poorly indented block, but you know it's just going to confuse the shit out of you a week later when you haven't been staring at it for like a day. srsly u guys.