About reading Python code

W

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

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.

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

Stargaming

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.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.
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.python.org/lib/module-logging.html>`_ with the
``DEBUG`` level.

There was a blog post recently about how to do this `generically for
functions said:
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
 
W

WaterWalk

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.


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.


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


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

Ben C

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

Nir

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

Roman Dodin

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
 
G

Gabriel Genellina

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.

See the wiki at http://wiki.python.org/moin/DevelopmentTools for more
editors, debugging tools and IDEs.
 
S

sturlamolden

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.

First, I recommend that you write readable code! Don't use Python as
if you're entering the obfuscated C contest.

Two particularly important points:

* If you find yourself thinking this module is too long, that's
probably what it is. Half a page of code per module is fine. Two pages
of code per module can be too much.

* Comments are always helpful to the reader.

Second, I recommend getting a good IDE. E.g. pick one of:

* Microsoft Visual Studio (commercial)
* Eclipse with PyDev and CDT (free)
* SPE (free)
* ActiveState Komodo IDE (commercial)
 
P

Paul Rubin

WaterWalk said:
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.

Try winpdb.org which despite the name has nothing to do with MS Windows.
 
J

Jeff Schwab

sturlamolden said:
First, I recommend that you write readable code! Don't use Python as
if you're entering the obfuscated C contest.

Er, don't use anything other than C if you're entering an obfuscated C
contest, or anything other Perl for an obfuscated Perl contest, etc.

Do use Python if you're entering an obfuscated Python contest. Found
online:

[#[#[#[#[#[#[#[#[# By TaroOgawa #]#]#]#]#]#]#]#]#]


globals()
.update({ "______":
lambda x: globals()
.update(( dict([[x]
*2])))}), ______(((
"Just")))
,______(( "another"
)),______ ("Python"
),______( "Hacker")
];print ( " ".join(
[(Just),( (another)
),(Python ),Hacker]
));______

Two particularly important points:

* If you find yourself thinking this module is too long, that's
probably what it is. Half a page of code per module is fine. Two pages
of code per module can be too much.

* Comments are always helpful to the reader.

If only 'twere so. *Good* comments are always helpful. Please do not
comment every method with "this is a method" or the like. One of the
things that often makes newb code stand out is that the programmer
couldn't tell the things that needed to be commented from the places
where comments just got in the way.
Second, I recommend getting a good IDE. E.g. pick one of:

* Microsoft Visual Studio (commercial)
* Eclipse with PyDev and CDT (free)
* SPE (free)
* ActiveState Komodo IDE (commercial)

* Vim on Linux. (No desire here to debate the meaning of IDE, but with
Vim or Emacs on any Unix-like platform, the whole system effectively is
the IDE.)
 
H

hellt

First, I recommend that you write readable code! Don't use Python as
if you're entering the obfuscated C contest.

Two particularly important points:

* If you find yourself thinking this module is too long, that's
probably what it is. Half a page of code per module is fine. Two pages
of code per module can be too much.

* Comments are always helpful to the reader.

Second, I recommend getting a good IDE. E.g. pick one of:

* Microsoft Visual Studio (commercial)
* Eclipse with PyDev and CDT (free)
* SPE (free)
* ActiveState Komodo IDE (commercial)

under Microsoft Visual Studio do you mean IronPython instance?
 
P

Phil

First, I recommend that you write readable code! Don't use Python as
if you're entering the obfuscated C contest.

Two particularly important points:

* Comments are always helpful to the reader.

It would be nice if this was the case! I once saw a preogram where
a line of code like this:

foo++;

Would be annotated with a comment like this:

/****************************************************/
/* */
/* Increment foo */
/* */
/****************************************************/

This comment was worse than useless, because it (and others like
it) took up space that distracted from the information-containing
parts of the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top