About reading Python code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WaterWalk

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

    #2
    Re: About reading Python code

    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.

    Comment

    • Stargaming

      #3
      Re: About reading Python code

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

      Comment

      • WaterWalk

        #4
        Re: About reading Python code

        On Mar 17, 1:54 pm, Stargaming <stargam...@gma il.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_py thon.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.

        Comment

        • Ben C

          #5
          Re: About reading Python code

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

          Comment

          • Nir

            #6
            Re: About reading Python code

            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.

            Comment

            • Roman Dodin

              #7
              Re: About reading Python code



              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

              Comment

              • Gabriel Genellina

                #8
                Re: About reading Python code

                En Mon, 17 Mar 2008 01:54:01 -0200, WaterWalk <toolmaster@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

                Comment

                • sturlamolden

                  #9
                  Re: About reading Python code

                  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)

                  Comment

                  • Paul Rubin

                    #10
                    Re: About reading Python code

                    WaterWalk <toolmaster@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.

                    Comment

                    • hellt

                      #11
                      Re: About reading Python code

                      On 18 ÍÁÒ, 03:57, sturlamolden <sturlamol...@y ahoo.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?

                      Comment

                      • sturlamolden

                        #12
                        Re: About reading Python code

                        On 18 Mar, 08:00, hellt <Dodin.Ro...@gm ail.comwrote:
                        under Microsoft Visual Studio do you mean IronPython instance?
                        AFAIK, with the latest VS 2008 you can develop for CPython and
                        IronPython.





                        Comment

                        • Phil

                          #13
                          Re: About reading Python code

                          On 2008-03-18, sturlamolden <sturlamolden@y ahoo.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.

                          Comment

                          Working...