There are times when you want to write in one line using list comprehension, but you want to define variables, right? Yes, I would be happy if I could write it like this.
homu.let(lambda mami: (mami[0], mami[1] if len(mami) > 1 else None))
It is a story to realize it in half.
Reference: Implementing a LINQ-like list processing library in Python --TIM Labs
| It seems that you can do something like an extension method by overloading the operator. You can also make let using the same method!
class let:
def __init__(self, action):
self.action = action
def __ror__(self, source):
return self.action(source)
Then
homu | let(lambda mami: (mami[0], mami[1] if len(mami) > 1 else None))
I can write! Nice!
Recommended Posts