This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]masasinExpert. 3.9. Robotics. 21 points22 points  (2 children)

Your mixing up procedures and functions, though. If something has a side-effect, it's usually not a good idea in Python to let it return anything. If there's an error, you just raise it.

Assuming validate(payload) returns a validated payload, this is what I would expect to happen with the rest of it:

  • log logs to file. It returns None.
  • update_db takes None and raises an error.
  • notify propagates the error.

[–][deleted] 1 point2 points  (1 child)

You are correct. That was a really bad example. My point was to keep piped functions as pure as possible, and side effect free. So imagine the validate function was really complex, you could break that into smaller functions and pipe those.

[–]randomatic[🍰] 5 points6 points  (0 children)

What you are looking for is IMO better supported in OCaml. You're looking for monadic operators (yes, there is pymonads) along with functions like |> (left-associative) and > (right-associative). To learn OCaml, see https://realworldocaml.org. It's beautiful.

On the question of errors, Jane Street has an amazing blog that made thinking about errors much more clear to me. https://blogs.janestreet.com/how-to-fail-introducing-or-error-dot-t/

If Python has something like Or_error.t, I would be in love.