关于阅读Python代码 [英] About reading Python code

查看:78
本文介绍了关于阅读Python代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我想知道什么是有效的方法来弄清楚一块python代码是如何工作的。使用CI经常发现能够以步进模式运行代码并在调试器中设置断点非常有用所以我可以看看它是如何执行的,如何数据更改以及代码如何从一个函数跳转到另一个函数。但是使用Python,调试器是一个原始的
。默认的IDLE甚至不允许我设置

断点。当代码很长时,我经常迷失它。


所以我很好奇如何有效地阅读代码。我同意python代码

很清楚,但是当它变长时,阅读它仍然是一个很难的工作。

Hello. I wonder what''s the effective way of figuring out how a piece
of python code works. With C I often find it very useful to be able to
run the code in step mode and set breakpoints in a debugger so I can
watch how the it executes, how the data change and how the code jumps
from one function to another. But with Python, the debugger is a
little primitive. The default IDLE doesn''t even allow me to set a
breakpoint. When the code is long, I am often lost in it.

So I''m curious how to read code effectively. I agree that python code
is clear, but when it becomes long, reading it can still be a hard
work.

推荐答案

3月17日上午11:54,WaterWalk< toolmas ... @ 163.comwrote:
On Mar 17, 11:54 am, WaterWalk <toolmas...@163.comwrote:

您好。我想知道什么是有效的方法来弄清楚一块python代码是如何工作的。使用CI经常发现能够以步进模式运行代码并在调试器中设置断点非常有用所以我可以看看它是如何执行的,如何数据更改以及代码如何从一个函数跳转到另一个函数。但是使用Python,调试器是一个原始的
。默认的IDLE甚至不允许我设置

断点。当代码很长时,我经常迷失它。


所以我很好奇如何有效地阅读代码。我同意python代码

很清楚,但是当它变长时,阅读它仍然是一个很难的工作。
Hello. I wonder what''s the effective way of figuring out how a piece
of python code works. With C I often find it very useful to be able to
run the code in step mode and set breakpoints in a debugger so I can
watch how the it executes, how the data change and how the code jumps
from one function to another. But with Python, the debugger is a
little primitive. The default IDLE doesn''t even allow me to set a
breakpoint. When the code is long, I am often lost in it.

So I''m curious how to read code effectively. I agree that python code
is clear, but when it becomes long, reading it can still be a hard
work.



BTW。我认为这个问题在其他脚本语言中也存在。

BTW. I think this problem also exists in other script languages.


On Sun,2008年3月16日20:54:01 -0700,WaterWalk写道:
On Sun, 16 Mar 2008 20:54:01 -0700, WaterWalk wrote:

您好。我想知道什么才能找出一段如何运行python代码的有效方法。
Hello. I wonder what''s the effective way of figuring out how a piece of
python code works.



如果您的Python代码编写得很好,只需阅读它就可以轻松搞清楚它是什么意思

。对于更复杂的程序,当然,这个

方法可能会失败。

If your Python code is well-written, it should be easy figuring out what
it means by just reading it. For more complex programs, of course, this
method can fail.


CI通常会发现能够运行<非常有用br />
步进模式下的代码并在调试器中设置断点,这样我就可以看到它是如何执行的,数据如何变化以及代码如何从一个跳转到

对另一个人起作用。但是使用Python,调试器有点原始的
。默认的IDLE甚至不允许我设置断点。

当代码很长时,我经常迷失它。
With C I often find it very useful to be able to run
the code in step mode and set breakpoints in a debugger so I can watch
how the it executes, how the data change and how the code jumps from one
function to another. But with Python, the debugger is a little
primitive. The default IDLE doesn''t even allow me to set a breakpoint.
When the code is long, I am often lost in it.



IDLE就是一个包含电池的编辑器。有很多人

(包括我在内)做*不喜欢它因为它太弱而没有

任何真正的用例如果你的程序变得足够复杂(因为

IDLE本身足够原始)。


您可能对* real *调试器感兴趣,例如Python Debugger

`PDB< http://docs.python.org/lib/module-pdb.html>`_。如果你没有发现它的用法显而易见,那么快速谷歌搜索就会发现一个不错的教程

< http://www.ferg.org/ papers / debugging_in_python.html>`_。


您可能会发现`Python Profilers< http://docs.python.org/lib/

profile.html>`_特别有趣,`profile`找出哪个

函数吸收了最多的调用/时间,`trace`用于更复杂的

的东西。

`pythontracer< http://代码。 google.com/p/pythontracer/>`听起来像

两者的良好组合。

IDLE is just, well, a batteries-included editor. There are many people
(me included) who do *not* like it because it''s so weak and doesn''t have
any real uses cases if your programs get sufficiently complex (because
IDLE itself is sufficiently primitive).

You might be interested in *real* debuggers, such as the Python Debugger
`PDB <http://docs.python.org/lib/module-pdb.html>`_. If you don''t find
its usage obvious, a quick google search just turned up a nice `tutorial
<http://www.ferg.org/papers/debugging_in_python.html>`_.

You might find the `Python Profilers <http://docs.python.org/lib/
profile.html>`_ particularly interesting, `profile` for finding out which
function sucks up the most calls/time, `trace` for more sophisticated
stuff.
`pythontracer <http://code.google.com/p/pythontracer/>` sounds like a
good combination of both.


所以我很好奇有效地阅读代码。我同意python代码是明确的,但是当它变长时,阅读它仍然是一项艰苦的工作。
So I''m curious how to read code effectively. I agree that python code is
clear, but when it becomes long, reading it can still be a hard work.



一个常见的做法就是插入`print`语句,因为它很简单。如果您认为您的调试不是暂时的但可能有用且

将不时启用,您还可以使用`logging

模块< http ://docs.python.org/lib/module-logging.html>`_带

``DEBUG``等级。


有最近关于如何做到这一点的博客文章一般用于

函数< http://wordaligned.org/articles/echo>`_。

A common practice is just inserting `print` statements since it''s so
easy. If you think your debugging isn''t temporary but could be useful and
will be enabled every now and then, you could also use the `logging
module <http://docs.python.org/lib/module-logging.html>`_ with the
``DEBUG`` level.

There was a blog post recently about how to do this `generically for
functions <http://wordaligned.org/articles/echo>`_.


BTW。我认为这个问题在其他脚本语言中也存在。
BTW. I think this problem also exists in other script languages.



FWIW,我认为在脚本语言中特别容易实现各种跟踪(除了调试,这是相反

在Python中不受欢迎)因为你的机器和你的代码之间有一个*额外*级别(解释器)




HTH,

Stargaming

FWIW, I think it''s particularly easier in scripting languages to
implement all kinds of tracing (apart from debugging, which is rather
unpopular in Python) because you have one *extra* level (the interpreter)
between your machine and your code.

HTH,
Stargaming


3月17日下午1:54,Stargaming< stargam ... @ gmail.comwrote:
On Mar 17, 1:54 pm, Stargaming <stargam...@gmail.comwrote:

On Sun,2008年3月16日20:54:01 -0700,WaterWalk写道:
On Sun, 16 Mar 2008 20:54:01 -0700, WaterWalk wrote:

你好。我想知道什么才能找出一段如何运行python代码的有效方法。
Hello. I wonder what''s the effective way of figuring out how a piece of
python code works.



如果您的Python代码编写得很好,只需阅读它就可以轻松搞清楚它是什么意思

。对于更复杂的程序,当然,这个

方法可能会失败。


If your Python code is well-written, it should be easy figuring out what
it means by just reading it. For more complex programs, of course, this
method can fail.


CI通常会发现能够运行<非常有用br />
步进模式下的代码并在调试器中设置断点,这样我就可以看到它是如何执行的,数据如何变化以及代码如何从一个跳转到

对另一个人起作用。但是使用Python,调试器有点原始的
。默认的IDLE甚至不允许我设置断点。

当代码很长时,我经常迷失它。
With C I often find it very useful to be able to run
the code in step mode and set breakpoints in a debugger so I can watch
how the it executes, how the data change and how the code jumps from one
function to another. But with Python, the debugger is a little
primitive. The default IDLE doesn''t even allow me to set a breakpoint.
When the code is long, I am often lost in it.



IDLE就是一个包含电池的编辑器。有很多人

(包括我在内)做*不喜欢它因为它太弱而没有

任何真正的用例如果你的程序变得足够复杂(因为

IDLE本身足够原始)。


您可能对* real *调试器感兴趣,例如Python Debugger

`PDB< http://docs.python.org/lib/module-pdb.html>`_。如果你没有发现它的用法显而易见,那么快速谷歌搜索就会发现一个不错的教程

< http://www.ferg.org/ papers / debugging_in_python.html>`_。


您可能会发现`Python Profilers< http://docs.python.org/lib/

profile.html>`_特别有趣,`profile`找出哪个

函数吸收了最多的调用/时间,`trace`用于更复杂的

的东西。

`pythontracer< http://代码。 google.com/p/pythontracer/>`听起来像

两者的良好组合。


IDLE is just, well, a batteries-included editor. There are many people
(me included) who do *not* like it because it''s so weak and doesn''t have
any real uses cases if your programs get sufficiently complex (because
IDLE itself is sufficiently primitive).

You might be interested in *real* debuggers, such as the Python Debugger
`PDB <http://docs.python.org/lib/module-pdb.html>`_. If you don''t find
its usage obvious, a quick google search just turned up a nice `tutorial
<http://www.ferg.org/papers/debugging_in_python.html>`_.

You might find the `Python Profilers <http://docs.python.org/lib/
profile.html>`_ particularly interesting, `profile` for finding out which
function sucks up the most calls/time, `trace` for more sophisticated
stuff.
`pythontracer <http://code.google.com/p/pythontracer/>` sounds like a
good combination of both.


所以我很好奇有效地阅读代码。我同意python代码是明确的,但是当它变长时,阅读它仍然是一项艰苦的工作。
So I''m curious how to read code effectively. I agree that python code is
clear, but when it becomes long, reading it can still be a hard work.



一个常见的做法就是插入`print`语句,因为它很简单。如果您认为您的调试不是暂时的但可能有用且

将不时启用,您还可以使用`logging

模块< http ://docs.python.org/lib/module-logging.html>`_带

``DEBUG``等级。


有最近关于如何做到这一点的博客文章一般用于

函数< http://wordaligned.org/articles/echo>`_。


A common practice is just inserting `print` statements since it''s so
easy. If you think your debugging isn''t temporary but could be useful and
will be enabled every now and then, you could also use the `logging
module <http://docs.python.org/lib/module-logging.html>`_ with the
``DEBUG`` level.

There was a blog post recently about how to do this `generically for
functions <http://wordaligned.org/articles/echo>`_.


BTW。我认为这个问题在其他脚本语言中也存在。
BTW. I think this problem also exists in other script languages.



FWIW,我认为在脚本语言中特别容易实现各种跟踪(除了调试,这是相反

在Python中不受欢迎)因为你的机器和你的代码之间有一个*额外*级别(解释器)




HTH,

Stargaming


FWIW, I think it''s particularly easier in scripting languages to
implement all kinds of tracing (apart from debugging, which is rather
unpopular in Python) because you have one *extra* level (the interpreter)
between your machine and your code.

HTH,
Stargaming



感谢您的回复。我会尝试的。我想我需要更多的练习来熟悉Python的习语。

Thanks for your informative reply. I''ll try them. I think I need more
practice to familiarize myself with those idioms of Python.


这篇关于关于阅读Python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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