Speed-up for loops

Ulrich Eckhardt eckhardt at satorlaser.com
Wed Sep 8 12:55:56 CEST 2010


BartC wrote:
> So 'range' is just a class like any other. And that a class is something
> you can blithely copy from one variable to another. And whenever you see
> 'range' anywhere, you can't always be certain that someone hasn't done:
> 
> range = 42
> 
> at some point.

True. I read an explanation here that IMHO pretty well explains what's going
on: The "range" above is a label, attached with a piece of string to an
object. The object in this case is the integer 42. The same object can have
multiple labels attached to it, so "foo = bar" will just create a new
label "foo" and attach it to the object that the label "bar" is already
attached to.

Note that I said object, which includes class instances like the int 42
above, but also the classes themselves, functions (or bound functions[1])
and modules (I hope I didn't miss any).

> That explains a lot about the difficulties of implementing Python
> efficiently.

Yes, "range" is not a keyword or something intrinsic to the interpreter. It
is just a name that is looked up whenever it is encountered, and that costs
time. However, you also get some nice flexibility at that cost.

Cheers!

Uli

[1] bound function = class function where the instance parameter is bound.
Example:

  x = []
  a = x.append
  a(42) # calls x.append(42)

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list