Why would I get a TypeEror?

Terry Reedy tjreedy at udel.edu
Wed Jan 12 19:34:48 EST 2005

"Peter Hansen" <peter at engcorp.com> wrote in message 
news:XOOdnSCB9NmJ-HjcRVn-2w at powergate.ca...
> What did you expect the "length" of the integer 3 to be?

Perhaps 2 (bits in a minimal binary representation).

I once, for maybe a minute, considered proposing this as an overloaded 
meaning of len, but realized that that would more often mask errors than 
save time.

If case bits(i) is what the OP wants:

def bits(i):
  i = abs(i)
  b = 0
  while i:
    i >>= 1
    b += 1
  return b

Terry J. Reedy

More information about the Python-list mailing list