473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

About reading Python code

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.
Mar 17 '08 #1
12 1721
On Mar 17, 11:54 am, WaterWalk <toolmas...@163.comwrote:
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.
Mar 17 '08 #2
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.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.
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 <http://wordaligned.org/articles/echo>`_.
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
Mar 17 '08 #3
On Mar 17, 1:54 pm, Stargaming <stargam...@gmail.comwrote:
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.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.
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 <http://wordaligned.org/articles/echo>`_.
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.
Mar 17 '08 #4
On 2008-03-17, WaterWalk <to********@163.comwrote:
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.
Mar 17 '08 #5
Nir
On Mar 17, 5:54 am, WaterWalk <toolmas...@163.comwrote:
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.

Mar 17 '08 #6


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
Mar 17 '08 #7
En Mon, 17 Mar 2008 01:54:01 -0200, WaterWalk <to********@163.com>
escribi�:
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.

--
Gabriel Genellina

Mar 17 '08 #8
On 17 Mar, 04:54, WaterWalk <toolmas...@163.comwrote:
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)

Mar 18 '08 #9
WaterWalk <to********@163.comwrites:
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.
Mar 18 '08 #10
On 18 ÍÁÒ, 03:57, sturlamolden <sturlamol...@yahoo.nowrote:
On 17 Mar, 04:54, WaterWalk <toolmas...@163.comwrote:
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)
under Microsoft Visual Studio do you mean IronPython instance?
Mar 18 '08 #11
On 18 Mar, 08:00, hellt <Dodin.Ro...@gmail.comwrote:
under Microsoft Visual Studio do you mean IronPython instance?
AFAIK, with the latest VS 2008 you can develop for CPython and
IronPython.

http://blogs.msdn.com/haibo_luo/arch...6/5482940.aspx

Mar 18 '08 #12
On 2008-03-18, sturlamolden <st**********@yahoo.nowrote:
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.

Mar 25 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
5
by: b-blochl | last post by:
I wonder about the huge traffic in the question how many tuples are dispensible. I find that a question of only ternary or quarterny order - use it or use it not (free after Shakespears "to be or...
5
by: John Marshall | last post by:
Hi, Does anyone see a problem with doing: data = file("tata").read() Each time this is done, I see a new file descriptor allocated (Linux) but not released. 1) Will there ever be a point...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
23
by: Ray | last post by:
Hello! I've been reading about PyPy, but there are some things that I don't understand about it. I hope I can get some enlightenment in this newsgroup :) First, the intro: <excerpt> "The...
23
by: IOANNIS MANOLOUDIS | last post by:
I want to learn python. I plan to buy a book. I always find printed material more convenient than reading on-line tutorials. I don't know PERL or any other scripting language. I only know some...
56
by: infidel | last post by:
Where are they-who-hate-us-for-our-whitespace? Are "they" really that stupid/petty? Are "they" really out there at all? "They" almost sound like a mythical caste of tasteless heathens that "we"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.