Posts about internals

What the heck is an xrange?

Pop quiz: how would you implement Python’s xrange (known in Python 3.x as range) in Python and without making a list or any other sequence type?

If you answered “with a generator,” you’d be wrong! And it’s likely because you’ve only ever used an xrange in code like:

for i in xrange(10):
    # do something 10 times

In this case (where the xrange is only used as an “argument” to a for loop), a generator would probably suffice. But, in fact, xrange is implemented as an object, not a function or generator. Why? Let’s find out.

python, internals

Read More...

Exploring Python Code Objects

Inspired by David Beazley’s Keynote at PyCon, I’ve been digging around in code objects in Python lately. I don’t have a particular axe to grind, nor some particular task to solve (yet?), so consider this post just some notes and ramblings that might be of interest (and my apologies if not).

Disclaimer: This post is about CPython version 2.7, though much of it is also likely true for other CPython versions (including 3.x). I make no claims to its accuracy or applicability to PyPy, Jython, IronPython, etc.

python, internals, tinkering

Read More...