1 hour ago · Tech · hide · 0 comments

Python A-Z is a blog series about Python. Each day, I share insights, ideas and examples for different parts of Python development that match with the letter of the day. Blaugust is an annual blogging festival in August where the goal is to write a blog post every day of the month. Often in programming we end up in a situation where we want to check for an output of a function and then do something to it if it’s truthy. if get_next_step() is not None: execute(get_next_step()) To avoid calling the function twice and saving computing cycles, we would then call it first, save it and test against it: next_step = get_next_step() if next_step is not None: execute(next_step) In Python 3.8, we got an assignment operator (:=), nicknamed walrus operator because it looks like a cute walrus. It lets us shortcut this with if (next_step := get_next_step()) is not None: execute(next_step) It’s also handy in loops while (next_step := get_next_step()) is not None: execute(next_step) Now we skip two…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.