• Could not connect to Twitter

Get Going with Python (part) 3: Under the Hood – Objects and Debugging

[Update - Title changed to '(part) 3' to reflect the fact that I'm not discussing Python 3000.]

Welcome to the third part of my Get Going with Python series. Here we look “under Python’s hood” at objects and debugging.

  • Getting a Feel for Python
  • Python Development Environments (IDEs) parts 1 and 2
  • Python ‘under the hood’ – Objects and Debugging (this post)
  • GUI Development
  • Web development
  • Writing solid code with Unit Tests
  • Games and Physics
  • Python Advocacy

Getting familiar with Python’s internals takes a bit of time, I found the best way to understand what was happening behind the scenes is to use some of the built-in functions and read the docs at python.org.

The ‘id(object)‘ function tells us the unique identity of an object – you can use it to see the internal identifier for a variable, function or temporary variable. It is a great way to see how Python shares references to variables (e.g. a=22;b=a;id(a)==id(b)) and can give you an idea of what is happening behind the scenes.

Another useful function is ‘dir(object)‘. It tells us which names are defined in an object – you can use it on a module (e.g. import sys;dir(sys)), on a function (e.g. dir(sys.exit)), on a variable (e.g. a=23;dir(a)) and on a literal (e.g. dir(23)). This is a nice tool to understand what’s happening on the inside.

Jerol Harrington’s great 3-part Introduction to Python Objects should also give you a good understanding of the insides of Python objects.

To learn more about the insides of Python you should get friendly with a debugger. Python’s built-in pdb is good, we have a video of Rocky Bernstein’s pydb and the graphical WinPDB (video). ferg.org has a nice pdb tutorial, the concepts will transfer to all debuggers.

You might also want to buy access to my Python Newbies series – in this episode I give an example of using PyDev’s debugger inside Eclipse to step-through a program that is written in an earlier episode. During the episode I explain what’s happening with local variables, the stack and we investigate how variables are created and destroyed according to scope.

Finally – do some reading! Both the Python Reference Manual and the Python/C API Reference Manual will tell you more detail than you ever thought you needed to know :-)

Shameless plug – New Python developers will find my pay-to-access Python Newbies series very useful. About 2 hours of video cover the IDLE and PyDev development environments, running Python, writing code, debugging, refactoring, writing unit-tests and several exercises will test your new-found knowledge. To see how much other viewers liked the episodes see the comments on each video. Cost: £5 (approx. $10 US, 8 Euro).

ShowMeDo – Our 93+ Python videos are a great way to learn new skills and see tools in action. Please – leave a word of thanks and Vote for our authors if you find their videos helpful. Remember – feedback is fuel.

Please share:
  • DZone
  • del.icio.us
  • Reddit
  • Furl
  • Ma.gnolia
  • email
  • StumbleUpon
  • Technorati
  • TwitThis
  • Slashdot
If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

  1. Introduction to Python Objects Part 3 Now Available
  2. Introduction to Python Objects Part 2 Now Available
  3. New Club series for Python Beginners – Common Variables and Containers
  4. Python Beginners – Common Variables and Containers Series Completed
  5. More Club videos for Python Beginners – Common Variables and Containers

5 comments to Get Going with Python (part) 3: Under the Hood – Objects and Debugging