在Eclipse中缩进python文件(带pydev) [英] indent python file (with pydev) in eclipse

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

问题描述

我是日食中的新手。我想缩进我的代码的所有行,通过按快捷方式格式化打开的文件或类似的东西...
我知道CTRL + SHIFT + F(因为它实际上不工作在pydev! )
我一直在搜索几个小时,没有成功。在日食中有没有办法做到这一点。像CTRL + K,D在visual studio中,自动格式化和缩进所有源代码行?

解决方案

不要以为这个问题是有道理的。缩进是Python中的语法。让IDE自动缩进您的代码是没有意义的。如果它没有正确缩进,它不起作用,IDE不能知道您的缩进块的开始和结束。例如:

 #有效代码
对于范围(10)中的i:
b = i

在范围(b)中的j:
c = j

#还有效代码。
for i in range(10):
b = i

for range(b):
c = j

IDE没有可能的方法可以知道哪些是正确的版本,或者您的意图。如果要编写Python代码,您将不得不学习管理缩进。没有办法避免它,并期待IDE神奇地清理它,仍然得到所需的结果是非常不可能的。



进一步的例子: p>

 #有效代码。 
outputData = []

对于范围(100)中的i:
outputData.append(str(i))

print''。 (outputData)

#再次,也是有效的代码,行为截然不同。
outputData = []

对于范围(100)中的i:
outputData.append(str(i))

print''。 (outputData)

第一个将生成字符串列表,然后将连接的结果打印到控制台1时间。第二个仍然会产生字符串列表,但是打印循环的每次迭代的累积加入结果 - 100个打印语句。两者都是100%的语法正确。他们没有问题。他们中的任何一个都可能是开发者想要的。 IDE不能知道哪个是正确的。它可能非常容易地不正确地将第一个版本更改为第二个版本。因为语言使用缩进作为语法,所以没有办法配置IDE来为您执行此类格式。


I'm a newbie in eclipse. I want to indent all the lines of my code and formatting the open file by pressing a shortcut or something like that... I know the CTRL+SHIFT+F (as it actually doesn't work in pydev!!) I've been searching for hours with no success. Is there any way to do that in eclipse. kind of like CTRL+K,D in visual studio, which formats and indents all the source code lines automatically?

解决方案

I ... don't think this question makes sense. Indentation is syntax in Python. It doesn't make sense to have your IDE auto-indent your code. If it's not indented properly already, it doesn't work, and the IDE can't know where your indentation blocks begin and end. Take, for example:

# Valid Code
for i in range(10):
  b = i

for j in range(b):
  c = j

# Also Valid Code.
for i in range(10):
  b = i

  for j in range(b):
    c = j

There's no possible way that the IDE can know which of those is the correct version, or what your intent is. If you're going to write Python code, you're going to have to learn to manage the indentation. There's no way to avoid it, and expecting the IDE to magically clean it up and still get the desired result out of it is pretty much impossible.

Further example:

# Valid Code.
outputData = []

for i in range(100):
  outputData.append(str(i))

print ''.join(outputData)

# Again, also valid code, wildly different behavior.
outputData = []

for i in range(100):
  outputData.append(str(i))

  print ''.join(outputData)

The first will produce a list of strings, then print the joined result to the console 1 time. The second will still produce a list of strings, but prints the cumulative joined result for each iteration of the loop - 100 print statements. The two are both 100% syntactically correct. There's no problem with them. Either of them could be what the developer wanted. An IDE can't "know" which is correct. It could, very easily incorrectly change the first version to the second version. Because the Language uses Indentation as Syntax, there is no way to configure an IDE to perform this kind of formatting for you.

这篇关于在Eclipse中缩进python文件(带pydev)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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