←back to thread

320 points willm | 4 comments | | HN request time: 0s | source
Show context
dapperdrake ◴[] No.45106399[source]
Python's async is very difficult to use and debug. It seems to get stuck randomly, read like race conditions. And Python cannot work around this nicely with their lambdas only permitting a single expression in their body.

Not worth the trouble. Shell pipelines are way easier to use. Or simply waiting —no pun intended— for the synchronous to finish.

replies(1): >>45106678 #
1. mixmastamyk ◴[] No.45106678[source]
> lambdas only permitting a single expression

Use a tuple, maybe walrus, and return the last item[-1].

replies(1): >>45109314 #
2. dapperdrake ◴[] No.45109314[source]
That idea sounds good.

How do I get variables for not redoing long-running computations that depend on one-another? So, what if the third tuple value depends on the second and the second in turn depends on the first?

replies(2): >>45109470 #>>45110665 #
3. int_19h ◴[] No.45109470[source]
You can abuse list and sequence comprehensions for this. `for..in` is effectively a variable binding since you can target a freshly created list or a tuple if you need to bind a single value. So:

  [x
   for x in [some_complicated_expression]
   if x > 0
   for y in [x + 1]
   ...
  ][0]
That said, I wouldn't recommend this because of poor readability.
4. mixmastamyk ◴[] No.45110665[source]
That’s what walrus is for:

    future = lambda age: (
        print('Your age is:', age),
        older := age + 5,
        print('Your age in the future:', older),
        older,
    )[-1]

    print(future(20))

    # out
    Your age is: 20
    Your age in the future: 25
    25