In a previous article, you learned how to format your Python code for optimal readability. That piece went into detail about spacing, number of characters on a line, and more. In this article, you will learn how to keep your Python code explicit to ensure others fully understand your work and eliminate any possible confusion. This good practice will also make your code look and read well.
You should always write your code as explicitly as possible. In other words, you should be as literal as you can. This will ensure that there is no room for incorrect interpretation on the side of anyone reading your code, even when that is you.
In another article, we discussed common naming conventions. We’re now going to go over how naming conventions overlap with keeping your Python code specific and readable, without any confusion.
Make sure to choose names that are descriptive and leave no room for incorrect interpretation or ambiguity.
These aren’t great choices of variable names because they aren’t descriptive enough.
The function above, called “read,” will read some file in and have different behavior based on the file’s extension. This type of function can easily cause errors. We call this an implicit function because the information from the file extension modifies the code’s behavior.
Calling this “name” or “full name” is a much better practice than the one used in the previous one. The syntax “first name, last name” is called unpacking, and we can do this to unpack a collection of values into different variables; in this case, we’re unpacking the split elements of this string. We now have much more interpretable names for these values.
The name in this example is followed by explicit functions. “read_csv” and “read_json” are explicit functions, and they are specific for each type of data that we are trying to read. This can be easier for the user because the names make everything much clearer about what’s going on.
Another opportunity for using explicit rather than implicit syntax is how you should refer to objects imported from modules and packages. You should refer to a module when calling a function, class, or object from that module.
You’re able to import every single object within a module or package if you would like to, like in the example above, by using the import statement: from module import *. However, this is generally frowned upon because there can be name conflicts with existing objects in your namespace. There is no warning that your variables will be overwritten! When in doubt, it’s better to call the objects from the correct module or package explicitly.
For modules with very long names that you don’t want to write out repeatedly, you can use an alias to shorten the module or package name. Two good examples are the numpy (np) and pandas (pd) aliases, which are used universally. You can decide on your own aliases for other modules and packages.
Keeping your Python code explicit and specific is extremely important. Working these best practices into your daily coding will not only help strengthen your coding abilities, but it will also keep your code organized. This makes it easy for others to interpret what you’re trying to express.
All we’ve discussed today is just the beginning of keeping your code readable and interpretable. Readability in Python code is routinely stressed in our bootcamps and programs.
Ready to advance your programming skills to the next level? Check out this three-course program focused on building and advancing your Python Programming skills, or start your journey towards data science mastery by enrolling in our upcoming remote live and online data science bootcamps.