Note: This was originally posted at martinheinz.dev
Few weeks ago I posted an article (here) about some not so commonly known Python features and quite a few people seemed to like it, so here comes another round of Python features that you hopefully haven’t seen yet.
Naming a Slice Using slice Function
Using lots of hardcoded index values can quickly become a maintenance and readability mess. One option would be to use constants for all index values, but we can do better:
In this example, we can see that we can avoid mysterious indices, by first naming them using slice function and then using them when slicing out part of string. You can also get more information about the slice object using its attributes .start, .stop and .step.
Lots of command line tools or scripts require username and password to operate. So, if you happen to write such program you might find getpass module useful:
This very simple package allows you to prompt the user for a password as well as get their username, by extracting current user’s login name. Be aware though, that not every system supports hiding of passwords. Python will try to warn you about that, so just read warnings in command line.
Now, for a little more obscure feature of Python standard library. If you ever find yourself in situation where you need to find words similar to some input string using something like Levenshtein distance Python and difflib have your back.
difflib.get_close_matches finds the best "good enough" matches. Here, first argument is being matched against the second one. We can also supply optional argument n which specifies maximum number of matches to be returned. Another available keyword argument cutoff (defaults to 0.6) can be set to change threshold for score of matched strings.
If you have to do some networking in Python you might find ipaddress module very useful. One use-case would be generating list of ip addresses from CIDR ( Classless Inter-Domain Routing):
Another nice feature are network membership checks of IP addresses:
There are plenty more interesting features that I will not go over as you can find those here. Be aware though, that there is only a limited interoperability between ipaddress module and other network-related modules. For example, you can't use instances of IPv4Network as address strings - they need to be converted using str first.
If you are one of the people who refuse to use IDE and are coding in Vim or Emacs, then you probably got into a situation where having debugger like in IDE would be useful. And you know what? you have one — just run your program with python3.8 -i - the -i launches interactive shell as soon as your program terminates and from there you can explore all variables and call functions. Neat, but how about actual debugger ( pdb)?
Let’s use following program ( script.py):
And run script with python3.8 -i script.py
We see where we crashed, now let’s set a breakpoint:
Now run it again:
Most of the time print statements and tracebacks are enough for debugging, but sometimes, you need to start poking around to get a sense of what’s happening inside your program. In these cases you can set breakpoint(s) and when you run the program, the execution will stop on the line of breakpoint and you can examine your program, e.g. list function args, evaluate expression, list variables or just step through as shown above. pdb is fully featured python shell so you can execute literary anything, but you will need some of the debugger commands which you can find here
One feature that is very common in programming languages, but not in Python, is function overloading. Even though you can’t overload normal functions, you can still (kinda) overload constructors using class methods:
You might be inclined to put all the logic of alternate constructors into __init__ and solve it using *args, **kwargs and bunch of if statements instead of using class methods. That could work, but it can become hard to read and hard to maintain. I would therefore recommend to put very little logic into __init__ and perform all the operations in separate methods/constructors. This way you will get code that is clean and clear both for the maintainer and user of the class.
Have you ever wrote a function that was performing expensive I/O operations or some fairly slow recursive function that could benefit from caching (memoizing) of its results? If you did, then there is easy solution to that using lru_cache from functools:
In this example we are doing GET requests that are being cached (up to 32 cached results). You can also see that we can inspect cache info of our function using cache_info method. The decorator also provides a clear_cache method for invalidating cached results. I want to also point out, that this should not be used with functions that have side-effects or ones that create mutable objects with each call.
Finding the most common items in list is pretty common task, which you could do using for cycle and dictionary (map), but that would be a waste of time as there is Counter class in collections module:
Under the hood, Counter is just a dictionary that maps items to number of occurrences, therefore you can use it as normal dict:
Besides that you can also use update(more_words) method to easily add more elements to counter. Another cool feature of Counter is that you can use mathematical operations (addition and subtraction) to combine and subtract instances of Counter.
I think that this time most of the tips I shared here can be useful pretty much everyday if you are working with Python, so I hope they will come in handy. Also, if you have any thoughts on these Python tips and tricks, or maybe if you know of any better ways of solving above problems, then let me know! 🙂