Mastering Python Code Quality with Pylint
Key Takeaways
The video demonstrates how to use Pylint, a static code analysis tool, to evaluate and improve Python code quality based on official style guidelines, specifically PEP 8. It covers installing Pylint, running it on code, and addressing common issues such as naming conventions, docstrings, and attribute initialization.
Full Transcript
what is going on guys welcome back in today's video we're going to learn how to properly use pilint which is a static code analysis tool for Python and it's going to allow us to write more professional and More Beautiful code so let us get right into it [Music] alright so pile end is essentially a tool that allows us to analyze and evaluate the quality of our code we could also see the beauty of our code based on the official python styling guidelines based on the pep 8 because when we write python code we should follow certain guidelines certain conventions for example basic naming conventions class name should be written in Pasco case function names variable name should be written in snake case but also some more General stuff like at the end of each file you should have a blank line those are conventions written in the official python styling guidelines I have one or two videos on this channel where I explain the guidelines and how to write professional python code and pylon is essentially a tool that automatically tells us how much a file how much our code conforms to those guidelines it rates it basically 10 being the maximum rating that we can get but it also gets into negative numbers so it tells us basically how much we stick to those guidelines and we should stick to those guidelines unless we have a reason not to for example if we are in an ecosystem where we already have other conventions but we we're going to talk about this here in this video as well so in order to use pylin we need to open up the command line and we need to First install it I'm going to do that by saying pip install pilot and once we have pollen installed we don't have to import it we can just write our python code because we're going to use this tool in the command line so I have prepared some sample code here I deliberately made some mistakes in terms of styling and in terms of conventions so I'm going to just write it here the way I have prepared it with a couple of intentional mistakes so you can see we create a class my person we have a knit function here knit method with the parameters name age and gender and what we do is we say self dot name equals name self.h equals h uh and then we get to this function here get name self Dot asked for name is going to be set to True when we ask for the name and then we're going to return self dot name and then down here we're going to just print my person we're going to create a person Mike who's 20 years old male uh and we're going to get the name of Mike and print it so in terms of functionality this works we have a class we have instance an instance of the class Mike 20 years old with a male gender we can get the name we can print it everything works fine so the code Works in terms of functionality however there are a couple of things here that are definitely not professional first of all the class name is not in Pascal case it's just uh in lowercase characters we have here a parameter that is not used in The Constructor so we have to provide it but we don't actually use it anywhere we also have here a variable first of all the variable name is not in snake case but not just that but it's also initialized here in a function that is not or in a method that is not the Constructor we also have the the method name here get name which is also not in snake case but in um Pasco case essentially and we also don't have a blank line in the end so if I now go ahead open up my command line navigate to this coding directory here I have a shortcut for that I'm going to navigate to current and here I'm going to now say I think the command was pilot just pilot and then main.py if I say that you can see the code has been rated at 0 out of 10 and we can see why that is the case Let Me Maybe zoom out a little bit it tells us exactly what the problems are we can see here first of all final new line missing also we don't have a doc string in the module we don't have a doc string for the class the class name my person does not conform to to Pascal case and the snake case convention is also violated and stuff like that and we also have you an attribute to find outside of init and we also should have um yeah here too few public methods we have a class with only one public method so it doesn't really make sense to have this as a class here we should add another method and because of that we get a scoring of zero now to be honest I'm not exactly sure when we get a negative rating I think I had a negative rating and at some point maybe if we mess up even more here I'm not sure about this um but this is definitely not professional we can see we get 0 out of 10 and the best thing that you want to have is 10 out of 10. now some of these arrows some of these mistakes some of these violations are already also hinted in pycharm so if I hover above this here you can see class name should use camel case convention camo case Pasco case essentially the same thing we can ignore errors like this or we can also just change this and what we're going to do now is we're going to step by step make this better and better and see how pile and reacts to our changes so let's just go ahead and implement the correct naming conventions let's call this class my person in the proper case let's call this um let's call this attribute here asked underscore 4 underscore name and let's also maybe not initialize it in get name but initialize it up here let's say self dot um asked for name is false by default and then also let's use the gender let's say self.gender equals gender and let's also change here the name to get name like this and of course we need to do the same thing down here since we're using the functions from above my person so now at least we have the basic naming conventions uh we have uh everything initialized in the init method we have the proper conventions for the variables we have all the parameters being used um what we should also do is we should add a new line here and just by doing this we should get a much higher score so let's just rerun the command and you can see now that we get 6 out of 10 you can also always see the difference here plus six point points because the last run was 0 out of ten now if I change something here again let's say for example I don't use the gender I remove it here again I rerun the code what you will see is first of all that I get a 4.44 out of 10 the previous run was 6 out of 10 so now I get Negative 1.56 uh rating so worse than last time so whatever I changed I should basically revert it to get the proper ranking again so let's go ahead um and we get six points again well if we look here at the the problems that we still have uh we can easily see why we don't get 10 points because we're missing a lot of documentation strings and we also have too few public methods still so what we can do here is we can just add another method for example something like get H Maybe and we can return self.h and that alone should already be enough to remove this last point and give us some additional rating points we can see now 6.67 but of course the problem is now I added a new function that also doesn't have a function string so I now have a different problem here um well for the documentation I do have a video on my channel where I explain how to write proper doc strings you can watch this if you want to now I'm just going to add some dark strings so that we have something I'm going to add a simple module doc string I'm going to say here my perfect code example this should eventually get 10 out of 10 points something like that then for the class we're going to say that this is just a simple person class and then for the individual methods we're going to provide a dark string inside of them we're going to say here the return value of get name is going to be uh the name the turns the name of the person something like that and let's copy this and paste it down here for the HD as well so like this um and that should actually if I'm not mistaken cover everything and we now have 10 out of 10 points now this is a very simple example usually you have files with much more code and much more classes and much more interconnections and statements and stuff like that but this is the basic way this tool works you run it you get a score you run it you change something you run it again you get a better score or a worse score and you have to adjust based on that now what if certain things uh you don't want to change them but you want to actually keep them so you want to basically say okay it's fine for me to not use all uh not use all the parameters here or it's fine to use a different case in this particular example because it fits with a certain um with a certain definition with a certain guideline that I have inside of my company so for example maybe uh every time I'm just making up something right now every time we have an attribute that contains ask in its name we have to write it in Pascal case that's what my boss told me so what I'm going to do is I'm going to say here ask for name and I'm gonna use the same name here now this is going to give me negative points so if I run this tree you can see I lose some points because this is not in the snake case naming style now what I can do is I can say okay it's fine but I have to do it that way because my boss tells me to do it that way I don't want this to influence my score so what I can do is I can add a comment here I can say pilint and I can say disable equals and here you now put the the name or the identifier of the uh convention the error that you want to disable and the one you want to disable is always here in parentheses so invalid Dash name we can just say here invalid Dash name I'm not sure if we have to do it here as well or only once let's see um there you go yeah 10 points because now we basically say okay we ignore this convention in this particular case now I don't think that this affects H so if I say self.h equals H and I use h here as well I think this should still uh yeah it still removes some points because for this particular line I don't have the exception this pilot disabling uh statement only affects this particular line in this particular attribute it doesn't uh mean that I can just call everything how I want now it just means that this particular line here will not affect my score at least not because of the invalid name error all right so what I want to do here in order to show you how we can actually do that is I want to copy the code that we had in the beginning this one here with all the mistakes we can run pilot again so let me just save this here let me run pilot again and let's just go ahead and get 10 points without changing anything how do we do that we basically just copy all of these here and we um we put them into the code as uh as an exception so we say here for example pilint disable in this case invalid name we can also say um I think we should be able to provide multiple disable statements here so disable equals missing class doc string I'm going to also add up here a pilot disable missing module dark string stuff like that what else do we have we have a class doc string I think we talked about this already missing final new line let's go ahead and say here pilot disable missing final new line what else do we have here we want to say pilot disable um disable attribute defined outside in it we also want to say here pilot disable missing function doc string um and then we also want to say pilot disable unused argument so again all these names that I'm writing down here they are written here in the parentheses now let's see if I run this how many of those are still left you can see we already get 6.25 points just by adding these Commons uh what do we have here disable Okay I mistyped something doc sting it should be Doc string so we should get even more points now we have one more problem too few public methods so let's just go ahead and say here disable equals two few public methods and that should be enough to get 10 points so we can do that as well so if you have certain things you want to stick to the guidelines but for example because of the way your company structured your code base is structured or something like that you have to stick to different coding conventions different naming conventions or you don't want to have blank lines or you don't want to have to use every parameter because you just want to keep them there for later use you can also just disable that to be able to still evaluate the code quality or code Beauty so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye foreign [Music]
Original Description
Today we learn about pylint, which is a Python tool that evaluates our code quality and beauty based on the official style guidelines.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: Tool Use & Function Calling
View skill →Related Reads
📰
📰
📰
📰
Loop Engineering: The Skill That Just Made Your AI Workflow Obsolete
Medium · Machine Learning
We built a real-time Pokémon TCG AR overlay for live streams and open-sourced everything
Medium · Deep Learning
The Python Roadmap for AI Engineering in 2026: What You Actually Need to Learn
Medium · Python
Your AI Knows Everything About React. It Knows Nothing About Your React Project.
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI