heapreplace, methodcaller

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bearophileHUGS@lycos.com

    heapreplace, methodcaller

    Hello, I'm experimenting more with Python 2.6 and its numerous
    changes.

    To improve name coherence I think this method of the heapq module:
    heapq.heaprepla ce(heap, item)

    can grow an alias in Python 2.6.1/2.7 and 3.0/3.1:
    heapq.heappoppu sh(heap, item)

    So later the heapreplace() name can be deprecated.

    The heapq can also become a Heap class (with methods named as the
    functions), with an optional key function; time ago I have written
    such class in the cookbook.

    ----------

    Regarding the operators module, this syntax:
    methodcaller('r eplace', 'old', 'new')

    Has this meaning:
    lambda s: s.replace('old' , 'new')

    I don't know if methodcaller() is faster than that lambda but:
    - It's not shorter;
    - For me it's not more readable;
    - If it's faster than the lambda, then maybe CPython can start
    performing a little more optimizations, like turning that tiny lambda
    into inlined code.

    Bye,
    bearophile
  • Marc 'BlackJack' Rintsch

    #2
    Re: heapreplace, methodcaller

    On Sat, 18 Oct 2008 07:01:26 -0700, bearophileHUGS wrote:
    Hello, I'm experimenting more with Python 2.6 and its numerous changes.
    >
    […]
    >
    Regarding the operators module, this syntax: methodcaller('r eplace',
    'old', 'new')
    >
    Has this meaning:
    lambda s: s.replace('old' , 'new')
    >
    I don't know if methodcaller() is faster than that lambda but:
    - It's not shorter;
    - For me it's not more readable;
    Then use the ``lambda``.

    Your example has just literal constants. Let's take this example:

    methodcaller(me th, arg_a, arg_b)

    which is expressed as ``lambda`` function:

    lambda x, m=meth, a=arg_a, b=arg_b: getattr(x, meth)(a, b)

    Which is longer and IMHO less readable than `methodcaller() `.
    - If it's faster than the lambda, then maybe CPython can start
    performing a little more optimizations, like turning that tiny lambda
    into inlined code.
    How? The functions in `operator` are meant to be passed directly or to
    to create other functions that are passed as HOFs into other functions.
    So the compiler doesn't know in which functions they are used in the end
    and it's possible that different functions are used there too.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Raymond Hettinger

      #3
      Re: heapreplace, methodcaller

      On Oct 18, 7:01 am, bearophileH...@ lycos.com wrote:
      To improve name coherence I think this method of the heapq module:
      heapq.heaprepla ce(heap, item)
      >
      can grow an alias in Python 2.6.1/2.7 and 3.0/3.1:
      heapq.heappoppu sh(heap, item)
      >
      So later the heapreplace() name can be deprecated.
      Too late for 2.6 and possibly too late and too disruptive for 3.0
      (which needs to minimize transitions from 2.6).


      Raymond

      Comment

      Working...