有没有一种方法可以将Python代码中的缩进转换为大括号? [英] Is there a way to convert indentation in Python code to braces?

查看:396
本文介绍了有没有一种方法可以将Python代码中的缩进转换为大括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一位完全盲人的程序员,想学习Python。不幸的是,代码块以不同的缩进级别表示的事实是一个主要的绊脚石。我想知道是否有可用的工具允许我使用花括号或其他代码块定界符编写代码,然后将该格式转换为Python解释程序可以使用的适当缩进表示形式?

I am a totally blind programmer who would like to learn Python. Unfortunately the fact that code blocks are represented with different levels of indentation is a major stumbling block. I was wondering if there were any tools available that would allow me to write code using braces or some other code block delimiter and then convert that format into a properly indented representation that the Python interpreter could use?

推荐答案

有一个针对您的问题的解决方案,该解决方案随python一起分发。 pindent.py ,它位于Windows安装目录下的Tools\Scripts目录中(我的路径是C:\Python25\Tools\Scripts),看来您必须从svn.python中获取它。 org (如果您在Linux或OSX上运行)。

There's a solution to your problem that is distributed with python itself. pindent.py, it's located in the Tools\Scripts directory in a windows install (my path to it is C:\Python25\Tools\Scripts), it looks like you'd have to grab it from svn.python.org if you are running on Linux or OSX.

它在关闭块时添加注释,或者如果注释被放入,则可以适当缩进代码。这是pindent使用命令输出的代码示例:

It adds comments when blocks are closed, or can properly indent code if comments are put in. Here's an example of the code outputted by pindent with the command:

pindent.py -c myfile.py

def foobar(a, b):
   if a == b:
       a = a+1
   elif a < b:
       b = b-1
       if b > a: a = a-1
       # end if
   else:
       print 'oops!'
   # end if
# end def foobar

原始的 myfile.py 是:

def foobar(a, b):
   if a == b:
       a = a+1
   elif a < b:
       b = b-1
       if b > a: a = a-1
   else:
       print 'oops!'

您还可以使用 pindent.py -r 根据注释插入正确的缩进(有关详细信息,请阅读pindent.py的标题),这应允许您在

You can also use pindent.py -r to insert the correct indentation based on comments (read the header of pindent.py for details), this should allow you to code in python without worrying about indentation.

例如,运行 pindent.py -r myfile.py 会转换以下内容 myfile.py 中的代码插入由 pindent.py -c 上面的示例:

For example, running pindent.py -r myfile.py will convert the following code in myfile.py into the same properly indented (and also commented) code as produced by the pindent.py -c example above:

def foobar(a, b):
if a == b:
a = a+1
elif a < b:
b = b-1
if b > a: a = a-1
# end if
else:
print 'oops!'
# end if
# end def foobar

我想了解一下最终使用的解决方案,如果您需要任何其他帮助,请对此帖子发表评论,并我会尽力帮助。

I'd be interested to learn what solution you end up using, if you require any further assistance, please comment on this post and I'll try to help.

这篇关于有没有一种方法可以将Python代码中的缩进转换为大括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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