Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Does hashlib support a file mode?

Reply
Thread Tools

Re: Does hashlib support a file mode?

 
 
Phlip
Guest
Posts: n/a
 
      07-06-2011
On Jul 6, 11:42*am, Andrew Berg <(E-Mail Removed)> 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.

>
> func() = whatever the function returns
> func = the function object itself (in Python, everything's an object)
>
> Maybe you have Python confused with another language (I don't know what
> exactly you mean by initial assignment). Typically one does not need
> more than one name for a function/method. When a function/method is
> defined, it gets created as a function object and occupies the namespace
> in which it's defined.


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.
 
Reply With Quote
 
 
 
 
geremy condra
Guest
Posts: n/a
 
      07-06-2011
On Wed, Jul 6, 2011 at 3:07 PM, Phlip <(E-Mail Removed)> wrote:
> On Jul 6, 11:42*am, Andrew Berg <(E-Mail Removed)> 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.

>>
>> func() = whatever the function returns
>> func = the function object itself (in Python, everything's an object)
>>
>> Maybe you have Python confused with another language (I don't know what
>> exactly you mean by initial assignment). Typically one does not need
>> more than one name for a function/method. When a function/method is
>> defined, it gets created as a function object and occupies the namespace
>> in which it's defined.

>
> 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.


Python doesn't do anything to the definition of call. If you call
hashlib.md5() twice, you get two objects:

>>> import hashlib
>>> m1 = hashlib.md5()
>>> m2 = hashlib.md5()
>>> id(m1)

139724897544712
>>> id(m2)

139724897544880

Geremy Condra
 
Reply With Quote
 
 
 
 
Mel
Guest
Posts: n/a
 
      07-06-2011
Phlip 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.


Actually, in

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

hashlib.md5 *is* called once; that is when the def statement is executed.

Later on, when file_to_hash gets called, the value of m is either used as
is, as the default parameter, or is replaced for the duration of the call by
another object supplied by the caller.

Mel.



 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      07-06-2011
On Wed, Jul 6, 2011 at 1:07 PM, Phlip <(E-Mail Removed)> 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/com....html#function
 
Reply With Quote
 
Anssi Saari
Guest
Posts: n/a
 
      07-07-2011
Mel <(E-Mail Removed)> 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].
 
Reply With Quote
 
Paul Rudin
Guest
Posts: n/a
 
      07-07-2011
Anssi Saari <(E-Mail Removed)> writes:

> Mel <(E-Mail Removed)> 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 = []
...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Does hashlib support a file mode? Carl Banks Python 13 07-09-2011 04:44 AM
Re: Does hashlib support a file mode? Phlip Python 6 07-06-2011 06:42 PM
Does hashlib support a file mode? Phlip Python 6 07-06-2011 01:47 PM
my small hashlib - using pythons hash-functions Mathias Panzenboeck Python 5 11-26-2006 12:18 AM
python 2.5 and hashlib rtilley Python 2 02-11-2006 08:19 PM



Advertisments