Back to Lesson

Safe Function Parsing

Reduce duplicate calls executing walrus assignments.

Instructions

  • Python 3.8+ introduced
    :=
    ! Allowing you to assert evaluations directly within conditional scopes.
  • Assume a mocked processing pipeline returning integer quantities. Function simulation:
    def get_count(): return 5
  • Instead of calling
    count = get_count()
    , and then
    if count > 0:
    ... combine them! Determine
    if (count := get_count()) > 0:
    .
  • If it enters the block, print
    f"{count} valid items"
    .
main.py
main.py