Does hashlib support a file mode?
Paul Rudin
paul.nospam at rudin.co.uk
Thu Jul 7 08:13:22 EDT 2011
Anssi Saari <as at sci.fi> writes:
> Mel <mwilson at the-wire.com> writes:
>
>> def file_to_hash(path, m = hashlib.md5()):
>>
>> hashlib.md5 *is* called once; that is when the def statement is executed.
>
> Very interesting, I certainly wasn't clear on this. So after that def,
> the created hashlib object is in the module's scope and can be
> accessed via file_to_hash.__defaults__[0].
This also why you have to be a bit careful if you use e.g. [] or {} as a
default argument - if you then modify these things within the function
you might not end up with what you expect - it's the same list or
dictionary each time the function is called. So to avoid that kind of
thing you end up with code like:
def foo(bar=None):
if bar is None:
bar = []
...
More information about the Python-list
mailing list