Modern Python Projects Transcripts
Chapter: Writing code
Lecture: How to *not* write a good Python code?
0:00 So far in this course, we learn how to set up our development environment and
0:04 how to start building a python project.
0:06 Now we can finally sit down and start writing some code. In this chapter,
0:12 I want to talk about how to write good python code.
0:15 I can't tell you exactly how to write the best python code,
0:18 but there are some rules that you can follow and some tools that can help you
0:21 follow those rules. I found this interesting reddit comment explaining that basically,
0:28 if your Python program doesn't have to throw any exceptions,
0:32 then you can write it in a single line without any semi colons.
0:36 You can check it out for the explanation of how it's possible,
0:39 but when we scroll down, you can see a code example here.
0:43 And as you can see, this whole code can be written one line.
0:46 So, as you can see,
0:47 you are free to write such a monstrosity like this.
0:51 But it's not gonna be the easiest thing to read.
0:54 And even if you actually split your code into multiple lines,
0:57 nothing stops you from using a different number of spaces than for or from using some
1:02 other crazy indentation. Take a look at this ugly code example.
1:07 The greet Function uses one space for indentation.
1:10 The create_fullname function uses eight spaces,
1:13 and when we call the greet function,
1:15 we use a completely inconsistent and hard to read indentation.
1:19 What happens when we try to run this code?
1:23 Python runs it without any problems.
1:25 There is no compiler that would give us atleast some warnings that your code is
1:29 an abomination, as long as there are no errors,
1:32 python wont complain. But we can't write code like that,
1:35 right? That's why some guidelines have been published and the two most popular ones are pep 8 and pep257.