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"), }; Rust ...

December 8, 2024 · 5 min

Syntax Errors in Logical Operators

Python favours letters and meaningful keywords for the logical operators, and all of them return a value depending on the conditions: not: The opposite of a condition, the reverse of the logical state of its operand. If the condition is True, it returns False, or vice versa. and: It needs to compare two operands, It returns True if both operands are True. or: It’s similar to and, but it returns True if at least one of the operands is True. ...

November 29, 2024 · 3 min

String quotes in PEP8

I use PEP8 in my Python projects except for a few rules. I think ‘single-quote’ and “double-quote” characters should NOT be the same. It could be better if these two characters had different counterparts in the Python language parser system. So we could use one of them for the template-strings (or f-strings). It doesn’t provide readability, causes inconsistency instead. Until now, I used string-quotes for the variable definitions and double-quotes for the visible texts. But now, I plan to use always double-quote as in many other programming languages. ...

January 12, 2019 · 1 min