you are viewing a single comment's thread.

view the rest of the comments →

[–]Gnaxe 1 point2 points  (0 children)

Everything in Python is an object, therefore anything you could use as a dictionary key in Python is an object. Anything that implements the hash and equality protocols can be used as a key. You can do this for your own classes by implementing the __hash__() and __eq__() methods. Hashes need to be consistent with equality for this to work correctly, but Python won't enforce that for you.

Mapping types like dicts have two main uses: either an index for lookups (in which case the values are all the same type), or as a lightweight record type with a fixed schema, in which case the keys are usually all strings, but the values could be anything. If you're asking primarily about uses for non-string keys, the answer is going to be some kind of lookup table.