检查Python代码的正确性

发布于 2024-08-04 01:23:32 字数 101 浏览 3 评论 0 原文

在 C++ 中,我有编译器告诉我重构后代码是否有问题。如何确保Python代码在修改后至少是正确的?可能存在一些愚蠢的错误,例如错误的函数名称等,在编译时很容易找到。

谢谢

In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

身边 2024-08-11 01:23:32

看起来像 PyCheckerpylint 就是您要找的

Looks like PyChecker or pylint are what you're looking for

神回复 2024-08-11 01:23:32
  1. 使用支持代码高亮的编辑器/IDE。例如,Notepad++ 有文字高亮功能,我觉得非常有用。

  2. 使用单元测试

愚蠢的错误将首先被淘汰,所以我不会太担心这种类型的错误。这是你应该害怕的“聪明”错误。

  1. use editor / IDE that supports code highlighting. E.g., Notepad++ has word-highlighting feature that I find very useful.

  2. use unit tests

stupid errors will be weeded out first, so I wouldn't worry to much about this type of errors. it's "smart" error you should be afraid of.

没有伤那来痛 2024-08-11 01:23:32
  1. 使用诸如pylintPyChecker

  2. 编写单元测试。

  1. Use tools such as pylint or PyChecker.

  2. Write unit tests.

芯好空 2024-08-11 01:23:32

单元测试。 http://docs.python.org/library/unittest.html

如果您的测试以合理的粒度级别编写,单元测试的速度可以与运行 lint 或编译器的速度一样快。

Unit test. http://docs.python.org/library/unittest.html

If your tests are written at a reasonable level of granularity, it can be as fast to unit test as it is to run lint or a compiler.

忆沫 2024-08-11 01:23:32
  • 静态分析(来自 IDE,或来自 pyLint 和 pyChecker 等工具)是检查简单错误并强制执行通用样式的一种非常快速且有效的方法。
  • 单元测试是确保代码遵守其合同的好方法。
  • 代码审查和结对编程是发现各种错误以及在团队中传播知识的最佳方法之一。

所有选项都需要一些时间来设置和执行。然而,收益是巨大的,远远高于投资。

  • Static analysis (as from the IDE, or from tools like pyLint and pyChecker) is a very quick and effective way to check simple errors, and enforce a common style.
  • Unit tests are a great way to ensure the code stands for its contract.
  • Code reviews and pair programming are one of the best ways to find errors of all sorts, and to spread knowledge in a team.

All of the options require some time, to setup and to execute. However, the gains are tremendous, and far higher than the investment.

时光无声 2024-08-11 01:23:32

Eclipse 有一个很好的 python 插件 用于进行语法突出显示和调试。

Eclipse has a good python plugin for doing the syntax highlighting and debugging.

帅的被狗咬 2024-08-11 01:23:32

Pylint 几乎正在做你正在寻找的事情。

您还可以强制编译 python 文件。这将显示一些基本语法错误(它不具备 C++ 编译器的所有功能)

我已阅读 这篇文章并决定用 pyDev 和 ant 制作一个自动化构建系统。它编译 python 文件并运行单元测试。下一步是将 pylint 集成到该流程中。

我希望它有帮助

Pylint is almost doing what you are looking for.

You can also force the compilation of your python files. That will show some basic syntax error (it doesn't have all the capability of a c++ compiler)

I've read this article and decided to make an automated build system with pyDev and ant. It does the compilation of the python files and is running the unit tests. Next step is to integrate pylint to that process.

I hope it helps

—━☆沉默づ 2024-08-11 01:23:32

与其他语言一样,您应该在整个代码中自由地使用断言。当必须依赖谓词为真才能运行程序时,请使用断言,而不是作为异常/错误处理。断言应该用于检查不可恢复的错误并强制程序崩溃。 有关断言(以及一般的 Python 错误检查)的更多信息

As with other languages, you should use assertions liberally throughout your code. Use assertions when you must rely on the predicate to be true for the program to run, not as exception/error handling. An assertion should be used to check for irrecoverable errors and force the program to crash. More on assertions (and python error checking in general)

谁的年少不轻狂 2024-08-11 01:23:32

你可能需要这个:

python -m py_compile script.py

You may need this:

python -m py_compile script.py
把昨日还给我 2024-08-11 01:23:32

您可能还想查看 PEP8 作为 Python 代码的风格指南。

You might also want to check out PEP8 as a style guide for Python Code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文