Re: Does hashlib support a file mode?

Discussion in 'Python' started by Phlip, Jul 6, 2011.

  1. Phlip

    Phlip Guest

    On Jul 6, 11:42 am, Andrew Berg <> 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.
     
    Phlip, Jul 6, 2011
    #1
    1. Advertising

  2. On Wed, Jul 6, 2011 at 3:07 PM, Phlip <> wrote:
    > On Jul 6, 11:42 am, Andrew Berg <> 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
     
    geremy condra, Jul 6, 2011
    #2
    1. Advertising

  3. Phlip

    Mel Guest

    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.
     
    Mel, Jul 6, 2011
    #3
  4. Phlip

    Ian Kelly Guest

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


    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
     
    Ian Kelly, Jul 6, 2011
    #4
  5. Phlip

    Anssi Saari Guest

    Mel <> 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].
     
    Anssi Saari, Jul 7, 2011
    #5
  6. Phlip

    Paul Rudin Guest

    Anssi Saari <> writes:

    > Mel <> 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 = []
    ...
     
    Paul Rudin, Jul 7, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John J Lee
    Replies:
    3
    Views:
    550
    bruno at modulix
    Dec 1, 2005
  2. Edward Loper
    Replies:
    0
    Views:
    523
    Edward Loper
    Aug 7, 2007
  3. Phlip
    Replies:
    6
    Views:
    1,455
    Phlip
    Jul 6, 2011
  4. Phlip
    Replies:
    6
    Views:
    451
    Andrew Berg
    Jul 6, 2011
  5. Carl Banks

    Re: Does hashlib support a file mode?

    Carl Banks, Jul 6, 2011, in forum: Python
    Replies:
    13
    Views:
    342
    Steven D'Aprano
    Jul 9, 2011
Loading...

Share This Page