Introduction
Python is a powerful programming language that’s easy to learn and fun to play with. But beyond the basics, there are plenty of hidden features and tricks that can help you write more efficient and more effective Python code.
In this article, we’ll uncover some of these hidden features and show you how to use them to improve your Python programming skills.
Exploring Python’s Hidden Features
Python is full of hidden features, some of which are more hidden than others. These features can be incredibly useful and can help you write more efficient and readable code. However, they can also be a bit tricky to discover if you don’t know where to look.
In the next few sections we’ll take a look at a couple features that are both helpful to know and not known as well throughout Python programmers.
The _ (Underscore) in Python
One of Python’s hidden gems is the underscore (_
). It’s a versatile character that can be used in various ways in Python code.
First, it can be used as a variable in Python. It’s often used as a throwaway variable, i.e., a variable that is being declared but the value is not actually used.
for _ in range(5):
print("Hello, World!")
In the above code, we’re not using the variable _
anywhere, it’s just there because a variable is required by the syntax of the for
loop.
Second, _
is used for ignoring the specific values. If you don’t need the specific values or the values are not used, just assign the values to underscore.
x, _, y = (1, 2, 3)
Here we need both the x
and y
variables, but Python’s syntax won’t let us declare them without something in betwee, so we use the underscore.
Last, in the Python console, _
represents the last executed expression value.
>>> 10 + 20
30
>>> _
30
Note: The use of _
for storing the last value is specific to Python’s interactive interpreter and won’t work in scripts!
Regex Debugging via Parse Tree
Regular expressions can be complex and hard to understand. Thankfully, Python provides a hidden feature to debug them via a parse tree. The re
module in Python provides the re.DEBUG
flag which can be used to debug regular expressions.
Consider the following code:
import re
re.compile("(d+).(d+)", re.DEBUG)
This will output:
SUBPATTERN 1 0 0
MAX_REPEAT 1 MAXREPEAT
IN
CATEGORY CATEGORY_DIGIT
LITERAL 46
SUBPATTERN 2 0 0
MAX_REPEAT 1 MAXREPEAT
IN
CATEGORY CATEGORY_DIGIT
0. INFO 4 0b0 3 MAXREPEAT (to 5)
5: MARK 0
7. REPEAT_ONE 9 1 MAXREPEAT (to 17)
11. IN 4 (to 16)
13. CATEGORY UNI_DIGIT
15. FAILURE
16: SUCCESS
17: MARK 1
19. LITERAL 0x2e ('.')
21. MARK 2
23. REPEAT_ONE 9 1 MAXREPEAT (to 33)
27. IN 4 (to 32)
29. CATEGORY UNI_DIGIT
31. FAILURE
32: SUCCESS
33: MARK 3
35. SUCCESS
re.compile('(\d+)\.(\d+)', re.DEBUG)
This is the parse tree of the regular expression. It shows that the regular expression has two subpatterns ((d+)
and (d+)
), separated by a literal dot (.
).
This can be incredibly useful when debugging complex regular expressions. It gives you a clear, visual representation of your regular expression, showing exactly what each part of the expression does.
Note: The re.DEBUG
flag does not work with the re.match()
or re.search()
functions. It only works with re.compile()
.
Ellipsis
Python’s ellipsis is a unique feature that’s not commonly seen in other programming languages. It’s represented by three consecutive dots (…) and it’s actually a built-in constant in Python. You might be wondering, what could this possibly be used for? Let’s explore some of its applications.
Python’s ellipsis can be used as a placeholder for code. This can be very useful when you’re sketching out a program structure but haven’t implemented all parts yet. For instance:
def my_func():
...
Here, the ellipsis indicates that my_func
is incomplete and needs to be implemented.
Python’s ellipsis also plays a role in slicing multi-dimensional arrays, especially in data science libraries like NumPy. Here’s how you can use it:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[..., 2])
This will output:
[[ 3 6]
[ 9 12]]
In this case, the ellipsis is used to access the third element of each sub-array in our 3D array.
The dir() Function
The dir()
function is another hidden gem in Python. It’s a powerful built-in function that returns a list of names in the current local scope or a list of attributes of an object.
When used without an argument, dir()
returns a list of names in the current local scope. Here’s an example:
x = 1
y = 2
print(dir())
This will print something like:
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'x', 'y']
Here, x
and y
are the variables we defined, and the rest are built