Does hashlib support a file mode?

Ian Kelly ian.g.kelly at
Wed Jul 6 21:48:36 CEST 2011

On Wed, Jul 6, 2011 at 1:07 PM, Phlip <phlip2005 at> wrote:
> If I call m = md5() twice, I expect two objects.
> I am now aware that Python bends the definition of "call" based on
> where the line occurs. Principle of least surprise.

There is no definition-bending.  The code:

def file_to_hash(path, m = hashlib.md5()):
    # do stuff...


does not call hashlib.md5 twice.  It calls it *once*, at the time the
file_to_hash function is defined.  The returned object is stored on
the function object, and that same object is passed into file_to_hash
as a default value each time the function is called.  See:

More information about the Python-list mailing list