Anssi Saari <a...@sci.fi> writes:

> Mel <mwil...@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 = []
   ...

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to