This post continues a series on “Pythonic” code. Pythonic code is code that fits well with the design of the Python language. The last post examined the property decorator as a technique to make beautiful classes. This fourth post will dive into Python’s built-in functions.
- The list comprehension
- The with statement
- The property decorator
- Built-in functions
- Using the standard library
- Leveraging packages
A couple of years ago, I made a challenge for myself to read through all of Python’s standard library. Today, I would generally suggest that people don’t do that (for reasons I documented in a blog post about the readthrough experience).
Even with that caution, there is some documentation that you absolutely should read: the built-in functions reference.
The built-in functions are the set
of functions that do not need to be imported.
They are globally available
in any Python source file.
As of this writing
(with Python 3.6.1 documentation),
the set includes 68 functions.
Some of these functions are extremely common
and well known to proficient Python programmers
If you want to move
from proficient to expert,
using a majority
of these functions
will make your Python code far more Pythonic.
I’m going to describe a few of my favorites to give you an idea of what is available. Once you look at these examples, I’d really encourage you to read the built-in function reference so you can discover your own favorites!
Let’s look at these:
any function takes a list
if anything in the list evaluates to
This is a useful function
for reducing a bunch
of boolean checks
into a single value.
I think I’ve used
any most often in cases
where I’m checking permissions
or other business rules.
>>> checks = [False, False, True, False] >>> status = any(checks) >>> status True
If you ever have a loop
and you also need an incrementing counter
for the body of your loop,
enumerate is your friend.
The function can return pairs
that contain an index
and the value
in the list.
You can also set a
start keyword argument
to modify the starting value of your index.
>>> fruits = ['apple', 'orange', 'kiwi', 'pear'] >>> for index, fruit in enumerate(fruits, start=1): ... print(index, fruit) ... (1, 'apple') (2, 'orange') (3, 'kiwi') (4, 'pear')
file keyword argument
rather than the odd
print >> sys.stderr 'help me' style.
>>> print('Hello World') Hello World >>> import sys >>> print('An error', file=sys.stderr) An error
range is great for producing lists of values.
I don’t have much to say about it.
This one is fairly self-explanatory. :)
>>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(5, 10) [5, 6, 7, 8, 9] >>> range(0, 10, 2) [0, 2, 4, 6, 8]
Lists in Python have an included
The downside of this method is that this list will be sorted
If you don’t like that behavior,
you can get a new list
by passing your original list
This function also has the ability
to take a
that will decide how things are sorted.
Have a list of colors that you wanted sorted
into a rainbow?
That’s entirely possible
key function knows how to sort colors
>>> numbers = [5, 2, 3, 1, 4] >>> ordered = sorted(numbers) >>> ordered [1, 2, 3, 4, 5] >>> numbers [5, 2, 3, 1, 4] >>> numbers.sort() >>> numbers [1, 2, 3, 4, 5]
So much more!
Covering five example functions doesn’t even reach 10% of what Python has built in to the core. I believe your code will be way better if you can learn some of these functions and how to apply them regularly as you make new things.
Be sure to check out the docs to boost your skill set, and check back in next time when I illuminate how cool the standard library is.
Learn about Python!
You can join my newsletter along with 1,000+ other developers to help you learn more about Django and Python.