GeistHaus
log in · sign up

Writing Python like it’s Rust

kobzol.github.io

You can check out a YouTube recording of a talk based on this blog post.

2 pages link to this URL
Better Enumerations in Python

When dealing with more than one language, we look for similar approaches across languages. In Rust, there’s a keyword called enum to define and use enumerations. It’s easy to use, let’s look at this example from the official document: enum IpAddrKind { V4, V6, } struct IpAddr { kind: IpAddrKind, address: String, } let home = IpAddr { kind: IpAddrKind::V4, address: String::from("127.0.0.1"), }; let loopback = IpAddr { kind: IpAddrKind::V6, address: String::from("::1"), }; If we want to write this code in Python, we can define two classes and two variables simply. Let’s start with a simple alternative first:

0 inbound links article en posts pythonrust CC BY-NC-SA 4.0