Hacker Newsnew | comments | show | ask | jobs | submit login

#7 surprised me. But after looking at the the bytecodes, it's clear why. The while-True adds 3 byte codes to the loop body, the while-constant (using any constant 'truthy' value) adds 1:

  >>> import dis
  >>> def a(): 
  ...   while 1: pass
  ... 
  >>> def b():
  ...   while True: pass
  ... 
  >>> dis.dis(a)
    2           0 SETUP_LOOP               3 (to 6)
          >>    3 JUMP_ABSOLUTE            3
          >>    6 LOAD_CONST               0 (None)
                9 RETURN_VALUE        
  >>> dis.dis(b)
    2           0 SETUP_LOOP              10 (to 13)
          >>    3 LOAD_GLOBAL              0 (True)
                6 POP_JUMP_IF_FALSE       12
                9 JUMP_ABSOLUTE            3
          >>   12 POP_BLOCK           
          >>   13 LOAD_CONST               0 (None)
               16 RETURN_VALUE



Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: