Does hashlib support a file mode?

Ethan Furman ethan at stoneleaf.us
Wed Jul 6 16:24:31 EDT 2011


Phlip wrote:
>> On 2011.07.06 12:38 PM, Phlip wrote:
>>> Python sucks. m = md5() looks like an initial assignment, not a
>>> special magic storage mode. Principle of least surprise fail, and
>>> principle of most helpful default behavior fail.
 >>>
> 
> If I call m = md5() twice, I expect two objects.

You didn't call md5 twice -- you called it once when you defined the 
function.

Phlips naive code:
---
def file_to_hash(path, m = hashlib.md5()):
                        \---------------/
                         happens once, when
                         def line is executed


If you want separate md5 objects, don't create just one when you create 
the function, create one inside the function:

def file_to_hash(path, m = None):
     if m is None:
         m = hashlib.md5()


You should try the Principle of Learning the Language.

~Ethan~



More information about the Python-list mailing list