Modules are hashable?!

Alex Martelli aleaxit at yahoo.com
Fri Sep 3 04:30:17 EDT 2004


Maurice LING <mauriceling at acm.org> wrote:

> is there actually a practical reason to hash modules? can I call a 
> module using the hash key?

You cannot call a module: a module does not have a __call__ method.
This has nothing to do with hashing.

A practical reason to hash modules would be to associate to each module
some value or group of values -- without sticking those values in the
module itself -- or keep track of a set of modules having some special
characteristic whereby you want to "logically group them together" as
the set of keys into a certain dict (or in 2.4 the elements of a certain
set, since set is now a builtin type).

Consider for example a module that starts:

import foo, fee, fie, fo, fum, bar, baz, bat
yet_untested_modules = dict.fromkeys([ fee, bar, baz ])

later on you might have code like

if somemodule in yet_untested_modules:
    somemodule.perform_all_tests()
    del yet_untested_modules[somemodule]

for "just in time testing" of a certain set of modules.  OK, weird-ish,
but it's what I could come up in 20 seconds;-).  To have modules as dict
keys or set members they must be hashable, and since there's no reason
to make them unhashable, why not?


Alex



More information about the Python-list mailing list