DEV Community

HAP
HAP

Posted on

3

Python Throws a Bone to Other Developers

This may be a bit late to the party, but I recently found out that, starting with Python 3.8, You can assign variables within expressions. This was something I really disliked about Python way back in the 2.5 days when I switched over from C.

So, now instead of a code block like this:

if len(data) > 0:
    process(data)
print(f"Processed {len(data)} bytes!")
Enter fullscreen mode Exit fullscreen mode

You can combine variable assignment and the comparison in one line of code!

if (dat_len := len(data)) > 0:
    process(data)
print(f"Processed {dat_len} bytes!")
Enter fullscreen mode Exit fullscreen mode

It's all thanks to the addition of the := operator.

For those of you who started programming in Python, this may not be such a big deal. But for those of us who come to Python from other languages, this is a real benefit, IMHO, as it helps the transition of programming in a "Pythonic" way that is also similar to the programming patterns from other languages.

See the docs here.

Image of Datadog

Optimize UX with Real User Monitoring

Learn how Real User Monitoring (RUM) and Synthetic Testing provide full visibility into web and mobile performance. See best practices in action and discover why Datadog was named a Leader in the 2024 Gartner MQ for Digital Experience Monitoring.

Tap into UX Best Practices

Top comments (0)

ACI image

ACI.dev: Best Open-Source Composio Alternative (AI Agent Tooling)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Star our GitHub!

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay