Loading video player…

Managing and Measuring Python Code Quality (Overview)

Producing high-quality Python code involves using appropriate tools and consistently applying best practices. High-quality code is functional, readable, maintainable, efficient, and secure. It adheres to established standards and has excellent documentation.

You can achieve these qualities by following best practices such as descriptive naming, consistent coding style, modular design, and robust error handling. To help you with all this, you can use tools such as linters, formatters, and profilers.

By the end of this video course, you’ll understand that:

  • Checking the quality of Python code involves using tools like linters and static type checkers to ensure adherence to coding standards and detect potential errors.
  • Writing quality code in Python requires following best practices, such as clear naming conventions, modular design, and comprehensive testing.
  • Good Python code is characterized by readability, maintainability, efficiency, and adherence to standards like PEP 8.
  • Making Python code look good involves using formatters to ensure consistent styling and readability, aligning with established coding styles.
  • Making Python code readable means using descriptive names for variables, functions, classes, modules, and packages.

Read on to learn more about the strategies, tools, and best practices that will help you write high-quality Python code.

Download

Course Slides (.pdf)

823.7 KB
Download

Sample Code (.zip)

1.2 KB

00:00 Hello and welcome to the course Code Quality in Python. I’m Negar from Real Python, and I’ll be your guide throughout this video course. Your focus on this course, as you might have guessed, is learning how to write high-quality code and flag low-quality code.

00:16 What is even meant by high-quality code? High-quality code has certain characteristics to make sure it gets the job done while staying straightforward for you and your team to read, test, and maintain down the road.

00:30 Now you know the goal, but what characteristics are we talking here? What do you actually have to watch out for? Sometimes your intuition helps you out, and even sometimes it’s enough.

00:40 Look at these two code blocks, for example. You have two blocks of code that calculate the area of a rectangle, but which one is high-quality? The first block on the left shows a function called f that takes in two parameters a and b, and then it returns a multiplied by b.

00:58 You’re testing this function with inputs 5 as width and 3 as height, and you’re getting 15, which is 5 times 3, which is the correct answer. The code block on the right, however, has a function called rectangle_area().

01:13 Instead of a and b, you have width and height as your input names. You’re also checking the user input and validating whether they’re floats or not, and then you’re checking for edge cases. For example, what if the user has given you, say, negative values?

01:30 If so, you’re raising a ValueError. Finally, you’re returning width multiplied by height, same logic as the left code block.

01:39 You’re also testing it with 5 and 3 and getting 15, which is the correct result. So, which one is the higher-quality code? You probably guessed the right block, and you’d be right.

01:51 You probably intuitively knew that more readable and descriptive names and checking for edge cases are good. But sometimes intuition isn’t enough.

02:03 Look at your second case. You have two blocks of code that return the IDs that appear more than once. Both are readable, but which one is higher quality? The block on the left has a function that uses standard Python lists.

02:19 It loops through your IDs one by one, and for every unique ID that it sees, it uses the built-in .count() method to scan through the original list and check if that ID appears more than once.

02:32 If it does, it saves it to a duplicates list. The code block on the right has a function that uses sets instead of lists. It creates a memory log of IDs it has seen and a separate log for duplicates.

02:47 You’re testing both with a list called user_ids, and both of them return 1042, 1088, which are the correct IDs that are duplicates. Okay, so which one is the high-quality block?

03:02 This one is a bit trickier, right? You need more than your intuition. You need to actually do some analysis. Now, you’ll do a full analysis on this in later lessons, but if you chose the right block, you’d be right.

03:15 The big difference here is efficiency, and how well the function handles increasing problem sizes. Bottom line is, you can’t always rely on your intuition alone.

03:26 Sometimes code feels fine until you realize it completely falls apart at scale. Also, the term high-quality can sound a bit vague, right? That’s why you need a checklist of some sort to know which characteristics actually make the code high-quality, something you can keep coming back to as you write and review code.

03:46 That’s exactly why this course exists. In the next lessons, you’ll learn how to separate low-quality code from high-quality code and why it matters. You’ll write code that works and handles edge cases.

04:01 You’ll learn how to make your code readable so any developer or future you can open it and understand it instantly. You’ll also learn how to choose the right data structures and algorithms for efficient code.

04:13 And finally, you’ll learn how to spot and fix security mistakes, like unvalidated user input, before they become vulnerabilities.

04:23 Overall, your goal is to write code that lasts. Okay, ready? Next up is defining high-quality code.

Become a Member to join the conversation.