Numerous articles which teach python and it's libraries usually refer their code as "not being Pythonic enough" when reviewing it. But what does it actually mean? Let's explore it in this article.
Python is one of the most popular languages which is both easy to learn and complex enough to build a fully functional app in any field. It is a dynamically typed interpreted language which even though is built for higher readability, has its own perks. One of them is it's unique method of writing and structuring the code using a syntax exclusive to only Python itself.
When a programmer declares a snippet as not "Pythonic" enough, they basically mean that the code is either a literal translation from another language or that the code can be made more readable by using the inherent features of Python. Two examples are given to illustrate the concept further:
Tuple Packing
In Python, if there are a numbers of constants and values separated by a comma ' , ' then the Python interpreter automatically takes it as a collection data type known as tuple. This is commonly known as packing.
This packing utility is commonly used when we have to pass or receive a number of arguments but don't have a specific collective data type in mind to use. So for temporary cases tuple packing does a brilliant job in wrapping up these small use cases without much effort.
def sum_multiply_power(x, y):
a = x + y
b = x * y
c = x ** y
return a, b, c
ans = sum_multiply_power(5, 3)
print(ans)
>>> (8, 15, 125)
Similar to packing, Python also has tuple unpacking, through which we can extract elements of a collection data type into individual variables without any extra effort or hassle.
ans1, ans2, ans3 = ans
print("The first part of ans is:", ans1)
print("The second part of ans is:", ans2)
print("The third part of ans is:", ans3)
>>> The first part of ans is: 8
>>> The second part of ans is: 15
>>> The third part of ans is: 125
Also when passing in values to and fro a function or a variable, unpacking an element can also be done using the asterisk operator '*':
def print_3_birds(a, b, c):
print("At first comes:", a)
print("Then comes:", b)
print("And lastly comes:", c)
blues = "Jake", "Jay", "Jim"
print_3_birds(*blues)
>>> At first comes: Jake
>>> Then comes: Jay
>>> And lastly comes: Jim
List Comprehensions
List comprehensions are a unique way to add/update values inside a list data type and it also helps in creating a new list using values from a different list. List comprehension makes it much more readable and also reduces the complexity in understanding the working of the loops.
- List without using comprehension:
evens = []
for i in range(100):
if(i%2==0):
evens.append(i)
- List created using comprehension:
evens = [i for i in range(100) if i%2==0]
- List updated using comprehension:
odds = [i for i in range(100) if i%2==0]
odds = [num+1 for num in odds]
Another use case of list comprehension which comes in pretty handy is that it can also be used to call functions while iterating over a list of values.
def pretty_print(val):
print("The value of this variable is:", val)
[pretty_print(num) for num in (odds+evens)]
[print(num) for num in odds if odds>20]
In short, there is no limit to the Pythonic way of coding. Newer versions introduce new techniques, structure and methods for writing the same logic in more readable and more comprehensive manner. The best way to learn this coding style is to look into the Python documentations provided with each new version released and experiment with using different implementations for same logic.
Top comments (0)