在 Python 中使用空格有什么陷阱吗? [英] Are there any pitfalls with using whitespace in Python?

查看:27
本文介绍了在 Python 中使用空格有什么陷阱吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在 Python 中从来没有遇到过空格问题(虽然我只在两个项目中使用过,而且我是唯一的程序员).对于学习这门语言的人来说,Python 中的空格和缩进有哪些潜在的陷阱?

At the moment I have never had a problem with whitespace in Python (although I've only used it in two projects and I was the only programmer). What are some potential pitfalls with whitespace and indentation in Python for someone learning the language?

推荐答案

是的,有一些陷阱,但大多数时候,在实践中,它们变成了敌人 堂吉诃德风格的风车,即想象中的,在现实中没有什么可担心的.

yeah there are some pitfalls, but most of the time, in practice, they turn out to be enemy windmills of the Quixotic style, i.e. imaginary, and nothing to worry about in reality.

我估计最有可能遇到的陷阱是(包括确定的缓解步骤):

I would estimate that the pitfalls one is most likely to encounter are (including mitigating steps identified):

  1. 与他人合作,也就是协作

一个.如果您有其他人出于某种原因拒绝遵守 PEP 8,那么维护代码可能会变得很痛苦.一旦我向他们指出 Python 几乎通用的约定是 缩进级别 == 四个空格

a. if you have others which for whatever reason refuse to adhere to PEP 8, then it could become a pain to maintain code. I've never seen this in practice once I point out to them the almost universal convention for python is indent level == four spaces

B.让与您一起工作的任何人/每个人都接受约定并让他们弄清楚如何让他们的编辑器自动执行此操作(或者更好的是,如果您使用相同的编辑器,请向他们展示如何配置它),以便复制和粘贴和东西正常工作.

b. get anyone/everyone you work with to accept the convention and have them figure out how to have their editor automatically do it (or better yet, if you use the same editor, show them how to configure it) such that copy-and-paste and stuff just works.

必须投资一个不错"的编辑器而不是你当前的首选编辑器,如果你当前的首选编辑器不是 Python 友好的——这不是一个真正的陷阱,更多的是一个投资要求避免提到的与复制和粘贴、重构等相关的其他陷阱.停止使用记事本,你会在早上感谢自己.

having to invest in a "decent" editor other than your current preferred one, if your current preferred editor is not python friendly -- not really a pitfall, more an investment requirement to avoid the other pitfalls mentioned associated with copy-and-paste, re-factoring, etc. stop using Notepad and you'll thank yourself in the morning.

一个.在理解 python

B.大多数现代代码编辑器都能很好地处理python.我自己更喜欢 GNU Emacs,并且最近的版本带有出色的 python-mode 支持,开箱即用.有很多其他编辑器可供探索,包括许多免费的替代品和IDEs.

b. most modern code editors handle python decently. I myself prefer GNU Emacs, and recent versions come with excellent python-mode support out-of-the-box. The are plenty of other editors to explore, including many free alternatives and IDEs.

c.python 本身带有一个智能"python 编辑器,idle.如果您不熟悉,请检查它,因为它可能已经在您的 python 安装中可用,甚至可能比您当前的编辑器更好地支持 python.PyCrust 是在 Python 中实现的 Python 编辑器的另一种选择,它是 wxPython 的一部分.

c. python itself comes out of the box with a "smart" python editor, idle. Check it out if you are not familiar, as it is probably already available with your python install, and may even support python better than your current editor. PyCrust is another option for a python editor implemented in python, and comes as part of wxPython.

一些包含 python 的代码生成或模板环境(想想 HTML 生成或 python CGI/WSGI 应用程序)可能有怪癖

一个.他们中的大多数,如果他们接触到 python,已经采取措施尽量减少 python 的本质问题,但它仍然偶尔会弹出.

a. most of them, if they touch python, have taken steps to minimize the nature of python as an issue, but it still pops up once in a while.

B.如果您遇到这种情况,请熟悉框架作者已采取的措施以尽量减少影响,并阅读他们的建议(是的,如果在他们的项目中遇到过,他们会提供一些建议),并且很容易避免与 python 相关的陷阱.

b. if you encounter this, familiarize yourself with the steps that the framework authors have already taken to minimize the impact, and read their suggestions (and yes they will have some if it has ever been encountered in their project), and it will be simple to avoid the pitfalls related to python on this.

这篇关于在 Python 中使用空格有什么陷阱吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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