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