Skip to content

Commit

Permalink
new tip
Browse files Browse the repository at this point in the history
  • Loading branch information
bbelderbos committed Dec 14, 2023
1 parent d7b092a commit 58f3f67
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions notes/20231214205946.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 58f3f67

Please sign in to comment.