10 Time-saving Tips for Pythonists
Python is a beautiful language, able to inspire love in its users. So if you're looking to get into programming, or if you're a bit tired of C++, Perl, Java and the rest, I recommend you give Python a try.
Python has many features that make it attractive to programmers. It's easy to learn, object oriented, byte-compiled, free and open-source. It also has run-time type checking, full and fast support, and extensive libraries for performing various tasks.
Efficiency with Python
In this article, I want to highlight some of the ways you can save time in Python and thus maximize productivity.
In preparation, I quizzed several Pythonists on their best time-saving tips. Here are the results …
1. Don’t Use Semicolons
Since using semicolons is considered optional in Python—as opposed to other object-oriented programming languages—you don’t need to end each statement of your code with a semicolon.
This may seem simple and not really time wasting; but once your code stretches into thousands of lines, all these semicolons can get a bit distracting, and it's a lot of unnecessary typing.
2. Get a Good Code Editor
Choosing the right Python editor can be a huge time saver. Many new Python programmers are confused over which code editor to pick—especially with so many available.
It's very disruptive to get used to one editor and then have to change to another, so it's best to start with a good one. Make sure whichever you choose does flake8 and PEP8 in real time.
For guidance on which editor to choose, see my previous article on Which Code Editors Do Pythonists Use?
3. Follow the Python Code Style
Following the Python code style can enhance the readability of the code, and thus saves time while walking through your code. (The design philosophy of Python emphasizes the issue of code readability.)
4. Use the
help() is a very handy, built-in function that can save a lot of time—for example, when searching for descriptions of other functions. You can simply run this function directly from your interpreter console.
The Python documentation describes the ways you can put this function to good use.
5. Use Libraries
There are lots of Python libraries that save you from reinventing the wheel every time.
For example, you can choose from a range of packages offered by PyPI (the Python Package Index), a repository of Python software.
A nice example of a Python library is scikit-image. It enables image processing tasks like blurring, enhancing contrast and zooming—simply by calling functions.
6. Use Cookiecutter
Cookiecutter is a command-line utility that enables you to create all your Python project boilerplates from project templates, and this can be a big time saver.
7. Comment Rigorously
Getting in the habit of commenting your code will save you—and others—a lot of time, especially down the track. (Yes, we hear this a lot, but many programmers still need to be reminded, it seems!)
Commenting is particularly vital when working on teams—especially ones that change a lot.
8. Test Often
Try to test every component in your program. While it may sound time-consuming, testing as you code saves a lot of time in the long run, and reassures you that there are no hidden bugs. It also reinforces your understanding of what each piece of code is actually doing.
A REPL—a read-eval-print loop—is a common means of testing your code as you go, and is often employed by Pythonists.
9. Focus and Specialize
A common recommendation of accomplished Pythonists is to have a specialized focus or area of expertise. There are lots of things you can do with Python—from coding for web cams to dealing with computations and mathematics.
There are great libraries available to help with these tasks, such as SimpleCV, dealing with computer vision; Biopython, a library for biological computation; and SymPy, a library for dealing with symbolic mathematics.
Digging into areas like these, and mastering a particular framework, helps you learn Python at a deeper level, master a particular style of coding (mentioned in point 3), and deal with specialized types of problems.
10. Write Code Every Day
When you get in the habit of writing Python code every day, solving problems using Python will start to become second nature. You'll start to think in Python, so to speak, and this ultimately helps you to navigate and solve issues faster.
In this brief article, I've listed the main tips I gathered while talking with Pythonists about time-saving tricks. Here are a few others that I could have added to the list.
Attend Python Events and Meetups
Make sure to attend events and meetups where possible. They're great for sharing experiences, best practices, tools, and other interesting topics.
It may not be obvious that this is a time-saving strategy, but learning from the experiences of others—through advice, tips and hacks—is another way to avoid reinventing the wheel.
A great event to check out is the annual PyConf.
Think on Paper
Thinking on paper—before diving straight into code—will give you the flexibility to change. Going straight to code forces you to get involved in implementation details from the outset, which is not always the best use of your time when starting a project. Paper's distraction-free mode is great for brainstorming and problem solving!
Master the Fundamentals
Lastly, it may seem obvious, but make sure to invest time in learning the fundamentals of Python. This will eventually save you a lot of time, as you'll be better prepared to tackling more complex topics.
Some good books to help with this include
- Learning Python
- Python Programming for Beginners: An Introduction to the Python Computer Language and Computer Programming.
It's also important to keep abreast of news as it happens, by reading blogs and articles. A great blog to follow is The Mouse Vs. The Python.
Well, I'm sure there are more things that could be added to the list. What are your best time-saving tips when programming Python? Let me know in the comments!