Is there a Python equivalent of the Haskell 'let'

前端 未结 9 1268
青春惊慌失措
青春惊慌失措 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 22:09

    Only guessing at what Haskell does, here's the alternative. It uses what's known in Python as "list comprehension".

    [barcode(productId), metric(size)
        for (productId, (name, size)) in [
            (productId, lookup(productId)) for productId in list_]
    ]
    

    You could include the use of lambda:, as others have suggested.

提交回复
热议问题