From 58f3f67b352c7e090b9602971a524df3a41101a6 Mon Sep 17 00:00:00 2001 From: Bob Belderbos Date: Thu, 14 Dec 2023 21:00:28 +0100 Subject: [PATCH] new tip --- index.md | 1 + notes/20231214205946.md | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 notes/20231214205946.md diff --git a/index.md b/index.md index fec77d6..fd88aae 100644 --- a/index.md +++ b/index.md @@ -389,6 +389,7 @@ This file gets generated by [this script](index.py). - [How to capture the zen of python (and redirect standard output)?](notes/20231207123339.md) - [Readability counts.](notes/20231213183435.md) - [Sparse is better than dense.](notes/20231212143456.md) +- [Special cases aren't special enough to break the rules.](notes/20231214205946.md) - [Zip() got a strict keyword arg](notes/20220908171538.md) ## Zlib diff --git a/notes/20231214205946.md b/notes/20231214205946.md new file mode 100644 index 0000000..0fd2022 --- /dev/null +++ b/notes/20231214205946.md @@ -0,0 +1,18 @@ +# Special cases aren't special enough to break the rules. + +Consider the `capitalize_words()` function below. It uniformly uses the `.capitalize()` method for all words in a string, regardless of their original case. + +This approach exemplifies the principle by avoiding special handling for cases like acronyms or words that are already capitalized. + +This uniform treatment keeps the code simple and consistent, highlighting the elegance of Python's design philosophy. 🐍 😁 + +``` +def capitalize_words(s): + # no extra ifs, one consistent interface + return ' '.join(word.capitalize() for word in s.split()) + +print(capitalize_words("hello world")) # Outputs "Hello World" +print(capitalize_words("python is FUN")) # Outputs "Python Is Fun" +``` + +#zen