在没有制表符、缩进和空格标准的情况下,有没有办法在 Python 中编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/63086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way around coding in Python without the tab, indent & whitespace criteria?
提问by Donny V.
I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of setting to turn this off?
我想开始在小型项目中使用 Python,但错误放置的制表符或缩进可能会引发编译错误这一事实确实让我感到不安。是否有某种类型的设置可以关闭它?
I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
我目前正在使用 NotePad++。是否有一个 IDE 可以处理制表符和缩进?
采纳答案by Raz
I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?
我目前正在使用 NotePad++。是否有一个 IDE 可以处理制表符和缩进?
I liked pydevextensions of eclipse for that.
为此,我喜欢Eclipse 的 pydev扩展。
回答by Chris Calloway
from __future__ import braces
回答by Asaf Bartov
No. Indentation-as-grammar is an integral part of the Python language, for better and worse.
不。缩进作为语法是 Python 语言的一个组成部分,无论好坏。
回答by duane
Emacs! Seriously, its use of "tab is a command, not a character", is absolutely perfect for python development.
埃马克!说真的,它使用“tab is a command, not a character”,对于python开发来说绝对是完美的。
回答by Mark Biek
All of the whitespace issues I had when I was starting Python were the result mixing tabs and spaces. Once I configured everything to just use one or the other, I stopped having problems.
我在开始 Python 时遇到的所有空白问题都是混合制表符和空格的结果。一旦我将所有内容都配置为仅使用一种或另一种,我就不再遇到问题了。
In my case I configured UltraEdit & vim to use spaces in place of tabs.
就我而言,我将 UltraEdit 和 vim 配置为使用空格代替制表符。
回答by Andy Baker
I find it hard to understand when people flag this as a problem with Python. I took to it immediately and actually find it's one of my favourite 'features' of the language :)
我发现很难理解人们何时将此标记为 Python 的问题。我立即接受了它,实际上发现它是我最喜欢的语言“功能”之一:)
In other languages I have two jobs: 1. Fix the braces so the computer can parse my code 2. Fix the indentation so I can parse my code.
在其他语言中,我有两个工作: 1. 修复大括号,以便计算机可以解析我的代码 2. 修复缩进,以便我可以解析我的代码。
So in Python I have half as much to worry about ;-)
所以在 Python 中,我有一半的担心;-)
(nb the only time I ever have problem with indendation is when Python code is in a blog and a forum that messes with the white-space but this is happening less and less as the apps get smarter)
(注意,我唯一一次遇到缩进问题是当 Python 代码在博客和论坛中时会出现空白,但随着应用程序变得越来越智能,这种情况越来越少发生)
回答by Chris Conway
You should disable tab characters in your editor when you're working with Python (always, actually, IMHO, but especially when you're working with Python). Look for an option like "Use spaces for tabs": any decent editor should have one.
当您使用 Python 时,您应该在编辑器中禁用制表符(实际上,恕我直言,尤其是当您使用 Python 时)。寻找“使用空格作为标签”之类的选项:任何像样的编辑器都应该有一个。
回答by Chris Conway
It's possible to write a pre-processor which takes randomly-indented code with pseudo-python keywords like "endif" and "endwhile" and properly indents things. I had to do this when using python as an "ASP-like" language, because the whole notion of "indentation" gets a bit fuzzy in such an environment.
可以编写一个预处理器,它采用带有伪 python 关键字(如“endif”和“endwhile”)的随机缩进代码并正确缩进内容。当使用 python 作为“类似 ASP”的语言时,我不得不这样做,因为在这样的环境中,“缩进”的整个概念变得有点模糊。
Of course, even with such a thing you really ought to indent sanely, at which point the conveter becomes superfluous.
当然,即使有这样的事情,你真的应该理智地缩进,在这一点上,conveter 就变得多余了。
回答by Thomas Owens
I do not believe so, as Python is a whitespace-delimited language. Perhaps a text editor or IDE with auto-indentation would be of help. What are you currently using?
我不这么认为,因为 Python 是一种以空格分隔的语言。也许具有自动缩进功能的文本编辑器或 IDE 会有所帮助。您目前使用的是什么?