Does hashlib support a file mode?

Ian Kelly ian.g.kelly at gmail.com
Wed Jul 6 15:48:36 EDT 2011


On Wed, Jul 6, 2011 at 1:07 PM, Phlip <phlip2005 at gmail.com> 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...

file_to_hash(path1)
file_to_hash(path2)
"""

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:

http://docs.python.org/reference/compound_stmts.html#function



More information about the Python-list mailing list