The Zen of Python2:32 with Kenneth Love
While PEP 8 is all about the rules for structuring your code, PEP 20 is all about the inspiration behind how you write your Python. Sure, you need to solve a problem, but if you can solve it beautifully, that's even better.
Guiding the poetry of Python, PEP 20 is written in verse. 0:00 PEP 20, or The Zen of Python, influences a lot of how you think about Python code. 0:04 It's actually an Easter egg in Python itself that you can access from 0:10 inside any Python shell. 0:12 Let's check it out. 0:14 So, I'm just in my Python shell and I want to import the library this. 0:15 This is actually almost kind of a, a joke or an Easter egg. 0:20 I'll let you read the whole thing for 0:24 yourself on your own computer or in workspaces. 0:25 But we're going to talk about some of the more important lines, 0:28 not necessarily in any specific order. 0:31 Explicit is better than implicit. 0:34 Python doesn't like to do things in the background. 0:36 The language was designed so 0:38 that you have to specify what you want to do all the time. 0:40 This is why, for example, you can't add numbers to strings. 0:44 That would require changing the string to a number or 0:47 the number to a string behind the scenes. 0:49 This goes against Python's rule of avoiding implicit actions. 0:53 Readability counts. 0:56 Going along with rules of PEP 8, Python wants to be readable. 0:58 Anytime you find yourself trying to figure out what a chunk of code does, 1:01 not because you haven't seen the functions before, but because you can't tell which 1:05 variable's which, it's a good sign that you need more readable code. 1:08 White space and well-named variables, functions and 1:13 classes all lead to better readability. 1:15 Why worry about formatting when you can just add in comments? 1:18 Comments are where the insanity goes. 1:21 Clean code doesn't need nearly as many explanatory comments. 1:23 Special cases aren't special enough to break the rules. 1:27 This is advice to use similar approaches to similar problems. 1:30 This is also the reason that Python uses, say, the len function to get the length of 1:34 things instead of relying on each type to give its own solution to that problem. 1:38 It's the same for looping over items, deleting variables and 1:42 collection members, and many other common scenarios. 1:45 This rule has a corollary though, that practicality beats purity. 1:49 It's better to have a practical solution than one that 1:52 fits some false ideal of perfection. 1:55 Beautiful is better than ugly. 1:58 And lastly, my favorite and one of my reasons for 2:00 sticking with Python over every other language I've investigated, 2:02 your code should be pleasant to look at and read. 2:06 Following PEP 8 will help with this a lot. 2:08 No one wants to wade through a mess of single-letter variables, deeply-nested for 2:11 loops and conflicting comments just to fix a bug. 2:15 Coding should be pleasant. 2:18 Hopefully, these few tidbits, along with the rest of PEP 20, 2:20 will help you to write more pleasant Python code. 2:23 If you need to see the list again, just run import this in your Python shell. 2:25 Now let's get back to more utilitarian topics. 2:30
You need to sign up for Treehouse in order to download course files.Sign up