dictionary confusion...

Alex Martelli aleax at aleax.it
Sun Jul 14 03:56:01 EDT 2002


mirvine555 wrote:
        ...

>             statusTable[mc]=mc
>             statusTable[mc]={status:time}

these lines are overwriting the previous entry for mc each and
every pass -- change them into a single statement:

        statusTable.setdefault(mc,{})[status] = time


The setdefault method of dictionaries is equivalent to the older
idiom:

        d.setdefault(x, y) ...

        ===

        if not d.has_key(x): d[x] = y
        d[x] ...

except that setdefault marginally faster and more convenient.


Alex




More information about the Python-list mailing list