Is there a Python equivalent of the Haskell 'let'

前端 未结 9 1212
青春惊慌失措
青春惊慌失措 2020-12-29 21:38

Is there a Python equivalent of the Haskell \'let\' expression that would allow me to write something like:

list2 = [let (name,size)=lookup(productId) in (ba         


        
相关标签:
9条回答
  • 2020-12-29 21:53

    To get something vaguely comparable, you'll either need to do two comprehensions or maps, or define a new function. One approach that hasn't been suggested yet is to break it up into two lines like so. I believe this is somewhat readable; though probably defining your own function is the right way to go:

    pids_names_sizes = (pid, lookup(pid) for pid in list1)
    list2 = [(barcode(pid), metric(size)) for pid, (name, size) in pids_names_sizes]
    
    0 讨论(0)
  • 2020-12-29 21:55

    Since you asked for best readability you could consider the lambda-option but with a small twist: initialise the arguments. Here are various options I use myself, starting with the first I tried and ending with the one I use most now.

    Suppose we have a function (not shown) which gets data_structure as argument, and you need to get x from it repeatedly.

    First try (as per 2012 answer from huon):

    (lambda x:
        x * x + 42 * x)
      (data_structure['a']['b'])
    

    With multiple symbols this becomes less readable, so next I tried:

    (lambda x, y:
        x * x + 42 * x + y)
      (x = data_structure['a']['b'],
       y = 16)
    

    That is still not very readable as it repeats the symbolic names. So then I tried:

    (lambda x = data_structure['a']['b'],
            y = 16:
      x * x + 42 * x + y)()
    

    This almost reads as an 'let' expression. The positioning and formatting of the assignments is yours of course.

    This idiom is easily recognised by the starting '(' and the ending '()'.

    In functional expressions (also in Python), many parenthesis tend to pile up at the end. The odd one out '(' is easily spotted.

    0 讨论(0)
  • 2020-12-29 21:57
    class let:
        def __init__(self, var):
            self.x = var
    
        def __enter__(self):
            return self.x
    
        def __exit__(self, type, value, traceback):
            pass
    
    with let(os.path) as p:
        print(p)
    

    But this is effectively the same as p = os.path as p's scope is not confined to the with block. To achieve that, you'd need

    class let:
        def __init__(self, var):
            self.value = var
        def __enter__(self):
            return self
        def __exit__(self, type, value, traceback):
            del var.value
            var.value = None
    
    with let(os.path) as var:
        print(var.value)  # same as print(os.path)
    print(var.value)  # same as print(None)
    
    

    Here var.value will be None outside of the with block, but os.path within it.

    0 讨论(0)
  • Recent python versions allows multiple for clauses in a generator expression, so you can now do something like:

    list2 = [ barcode(productID), metric(size)
              for productID in list
              for (name,size) in (lookup(productID),) ]
    

    which is similar to what Haskell provides too:

    list2 = [ (barcode productID, metric size)
            | productID <- list
            , let (name,size) = lookup productID ]
    

    and denotationally equivalent to

    list2 = [ (barcode productID, metric size) 
            | productID <- list
            , (name,size) <- [lookup productID] ]
    
    0 讨论(0)
  • 2020-12-29 22:03

    Although you can simply write this as:

    list2 = [(barcode(pid), metric(lookup(pid)[1]))
             for pid in list]
    

    You could define LET yourself to get:

    list2 = [LET(('size', lookup(pid)[1]),
                 lambda o: (barcode(pid), metric(o.size)))
             for pid in list]
    

    or even:

    list2 = map(lambda pid: LET(('name_size', lookup(pid),
                                 'size', lambda o: o.name_size[1]),
                                lambda o: (barcode(pid), metric(o.size))),
                list)
    

    as follows:

    import types
    
    def _obj():
      return lambda: None
    
    def LET(bindings, body, env=None):
      '''Introduce local bindings.
      ex: LET(('a', 1,
               'b', 2),
              lambda o: [o.a, o.b])
      gives: [1, 2]
    
      Bindings down the chain can depend on
      the ones above them through a lambda.
      ex: LET(('a', 1,
               'b', lambda o: o.a + 1),
              lambda o: o.b)
      gives: 2
      '''
      if len(bindings) == 0:
        return body(env)
    
      env = env or _obj()
      k, v = bindings[:2]
      if isinstance(v, types.FunctionType):
        v = v(env)
    
      setattr(env, k, v)
      return LET(bindings[2:], body, env)
    
    0 讨论(0)
  • 2020-12-29 22:07

    The multiple for clauses in b0fh's answer is the style I have personally been using for a while now, as I believe it provides more clarity and doesn't clutter the namespace with temporary functions. However, if speed is an issue, it is important to remember that temporarily constructing a one element list takes notably longer than constructing a one-tuple.

    Comparing the speed of the various solutions in this thread, I found that the ugly lambda hack is slowest, followed by the nested generators and then the solution by b0fh. However, these were all surpassed by the one-tuple winner:

    list2 = [ barcode(productID), metric(size)
              for productID in list
              for (_, size) in (lookup(productID),) ]
    

    This may not be so relevant to the OP's question, but there are other cases where clarity can be greatly enhanced and speed gained in cases where one might wish to use a list comprehension, by using one-tuples instead of lists for dummy iterators.

    0 讨论(0)
提交回复
热议问题