[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

About reading Python code

toolmaster

3/17/2008 3:54:00 AM

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.
8 Answers

toolmaster

3/17/2008 3:57:00 AM

0

On Mar 17, 11:54 am, WaterWalk <toolmas...@163.com> wrote:
> 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. I think this problem also exists in other script languages.

Stargaming

3/17/2008 5:55:00 AM

0

On Sun, 16 Mar 2008 20:54:01 -0700, WaterWalk wrote:

> Hello. I wonder what's the effective way of figuring out how a piece of
> python code works.

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.

> 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 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.pytho...module-pd...`_. 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_pytho...`_.

You might find the `Python Profilers <http://docs.pytho...
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/pythont...` sounds like a
good combination of both.

> 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.

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.pytho...module-logging.html>`_ with the
``DEBUG`` level.

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

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

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

toolmaster

3/17/2008 8:13:00 AM

0

On Mar 17, 1:54 pm, Stargaming <stargam...@gmail.com> wrote:
> On Sun, 16 Mar 2008 20:54:01 -0700, WaterWalk wrote:
> > Hello. I wonder what's the effective way of figuring out how a piece of
> > python code works.
>
> 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.
>
> > 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 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.pytho...module-pd...`_. 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_pytho...`_.
>
> You might find the `Python Profilers <http://docs.pytho...
> 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/pythont...` sounds like a
> good combination of both.
>
> > 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.
>
> 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.pytho...module-logging.html>`_ with the
> ``DEBUG`` level.
>
> There was a blog post recently about how to do this `generically for
> functions <http://wordaligned.org/article...`_.
>
> > BTW. I think this problem also exists in other script languages.
>
> 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

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

Ben C

3/17/2008 8:49:00 AM

0

On 2008-03-17, WaterWalk <toolmaster@163.com> wrote:
> 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.

It does, you just right-click and go "set breakpoint". But yes IDLE is a
bit basic.

nir1408

3/17/2008 9:55:00 AM

0

On Mar 17, 5:54 am, WaterWalk <toolmas...@163.com> wrote:
> Hello. I wonder what's the effective way of figuring out how a piece
> ofpythoncode works. With C I often find it very useful to be able to
> run the code in step mode and set breakpoints in adebuggerso I can
> watch how the it executes, how the data change and how the code jumps
> from one function to another. But withPython, thedebuggeris 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 thatpythoncode
> is clear, but when it becomes long, reading it can still be a hard
> work.

Try Winpdb - www.winpdb.org (works on Linux as well). Don't forget to
send feedback.

Roman Dodin

3/17/2008 10:12:00 AM

0



WaterWalk пиÑ?еÑ?:
> 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.
>

You also can use free IDE (for example Eclipse) and PyDev plugin, which
includes comfortable Debugger

james.pablos

12/2/2011 12:11:00 PM

0

On Nov 30, 11:24 am, band beyond description
<everybody's.d...@that.rag.com> wrote:

> and apologies to folks with cancer, but Cain's not so far out of the
> woods from his own complicated battle with the disease that, even if he
> didn't have that laundry list of decrepitude you list above, some might
> consider that health issue alone a disqualification.

"my wife did not know I was sending that woman money every month"

That shit would not fly in the Pablos house. No sireeeee...

Dave Kelly

12/2/2011 4:31:00 PM

0


"James Pablum" <james.pablos@gmail.com> wrote in message

> That shit would not fly in the Pablos house. No sireeeee...

Your monthly "gift" to the pool boy doesn't count...uh...right?
Thought not.