What about an EXPLICIT naming scheme for built-ins?

Raymond Hettinger python at rcn.com
Thu Sep 9 17:44:44 CEST 2004


> def isorted(seq):
>     a = list(seq)
>     heapify(a)
>     while a: yield heappop(a)

Very nice.

I had actually meant to use a "while 1" in which case the try/except
makes more sense.  Going one step further and localizing the heappop()
call, you get:

def isorted(seq):
    a = list(seq)
    heapify(a)
    pop = heappop
    try:
        while 1:
            yield pop(a)
    except IndexError:
        pass

For clarity and conciseness, Alex's version wins easily.  For running
speed, dis.dis() shows how the second has a simpler, more efficient
inner loop:

[Alex's compact version]
        >>   25 LOAD_FAST                1 (a)
             28 JUMP_IF_FALSE           14 (to 45)
             31 POP_TOP             
             32 LOAD_GLOBAL              4 (heappop)
             35 LOAD_FAST                1 (a)
             38 CALL_FUNCTION            1
             41 YIELD_VALUE         
             42 JUMP_ABSOLUTE           25

[try/except version with localized call]
  7     >>   34 LOAD_FAST                2 (pop)
             37 LOAD_FAST                1 (a)
             40 CALL_FUNCTION            1
             43 YIELD_VALUE         
             44 JUMP_ABSOLUTE           34


Raymond Hettinger



More information about the Python-list mailing list