This article is a collection of tips that can lead to better Python code. It is aimed at all experience levels.
Logical Comparisons
Here we demonstrate how to adjust your comparison statements to use logical comparisons. This can allow for better code readability and even performance improvements in new versions of Python.
This can be used when comparing booleans or checking whether a variable is None
.
Normal Comparison:
|
|
Logical Comparison:
|
|
Storing Unique Lists
If you are storing values that are unique in a list, utilising a set
may bring some advantages to your code.
The first advantage is that any value stored in a set will always be unique, since duplicates are not stored. Second is that you gain a performance improvement when checking if a value exists in the set, since every value stored has O(1) look-up time. This allows the access time to remain the same no matter how many elements are stored.
Changing your list to a set may not be suitable for all projects, for one if you want to store multiple values that are the same; you will not be able to. Also if you want to store ordered values; you cannot, since values in a set are not stored in an ordered way.
Using a set in your code can be achieved by using the following code:
|
|
Be aware that converting a list into a set is a O(n) operation, so if you are only performing a few look-ups; it may actually make your code less performant. If your data is ordered there are also different ways of searching through data other than using a set, or performing linear search. For example you could use a binary search or use a data-structure like a tree.
Iterating Over A Dictionary
A common way of iterating over keys in a dictionary is by using the keys()
method. However, this is no longer needed in newer versions of Python.
Instead to iterate over the keys in a dictionary we can use the following method:
|
|
|
|
If we wanted to access both the keys and the values in our loop we could use the items()
method. This removes the need to perform two access operations on every element and can make the code’s function more clear.
|
|
|
|
Checking Types
If you come into the situation of needing to check whether a value is a specific type, you may find yourself using the type()
method. This however, may not be the best solution. Take this example:
|
|
If we want to check if animal_one
is cat we could do the following:
|
|
|
|
However, if we want to check if the value stored is a animal, we cannot use the type method. We can instead use the isinstance()
method.
|
|
|
|
Iterating Over Collections
There are many ways of iterating over elements in a list. The two shown below are the most commonly used methods:
|
|
|
|
The first way allows us to access the index and then access the element via the index. If we just wanted the element and not the index, we may want to use the other method instead.
If we wanted to access both the index and the element, we could use a better method. This method would also reduce the operations from two to one. To do this we can use the enumerate()
method. As shown below:
|
|
|
|
Using Dunders
Ever wonder how we can print objects other than strings, or how we can perform mathematical operations on numerical types. All of those features and more can be added to your classes, using something called “Dunders” or “Special Methods”. These are methods that are called by Python automatically but implemented by the developer.
Let’s take a look at a simple example, say we want to print an object directly to the users terminal. Normally we would get a object reference outputted, however in this code example we will not.
|
|
|
|
What happens when this is printed? Well we do not get the normal output of a object, instead we get our message that is returned in the __repr__
method. This is one of the “Dunder” methods. These methods are always wrapped by double underscores. This method overrides the print representation, so when we pass the object into a print statement Python will call this.
How about something more cool, like allowing for two objects to be added together. Normally not possible, unless we use a numerical type.
|
|
|
|
When this code is run we will see that a new Total
object is created, containing the result of both the totals from the two objects added together.
There are many different “Dunders” available in Python. They are all spoken about in the official documentation.
References
- https://docs.python.org/3/tutorial/datastructures.html#sets
- https://docs.python.org/3/library/stdtypes.html#set
- https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
- https://docs.python.org/3/library/functions.html#type
- https://docs.python.org/3/library/functions.html#isinstance
- https://docs.python.org/3/reference/datamodel.html#special-method-names