This is the fifth and final in a series of posts on common pythonic idioms, so if you haven’t seen the previous two, you should definitely go and give them a read.
Learning the idioms presented in this series has really improved my Python code. Not only do a lot of these idioms actually run faster in Python, but they increase the readability of your code tenfold. Code often gets read a lot more than it is written to and it should be maintainable by other programmers if you should ever for some reason stop working on it. For this reason readability really counts, and pythonic Python is fantastic for this.
In this post we will go over some ‘notable others’ that weren’t included in the previous posts, but are definitely worth taking a look at.
THE HONOURABLE MENTIONS
Naming conventions
It is important to adhere to naming conventions when programming, it only takes a few moments to remember them, so really there is no excuse.
#Naming conventions #Classes class CamelCaseClass: #class code #Functions def underscore_separated_function(): #function code #Variables underscore_separated_var = True #'Private' variables _underscore_prefix = True
Pretty simple, not much to say here. One thing however is that Python actually doesn’t actually have private variables like Java does, but prefixing variables with and underscore indicates that it shouldn’t just be called by other applications etc. It’s sort of like an honour code.
Imports
Python offers a variety of ways to import classes and packages, some however are better than others.
#imports #Don't do this from package import * var = package_function() #Try not to do this from package import package_function var = package_function() #This is best import package var = package.package_function()
Importing like this and using the package.package_function() syntax makes it a lot more clear where the functions you are using are coming from, which makes your code a hell of a lot easier to read.
Context managers
Context managers are a great way of avoiding forgetting things like closing files. They provide extra information; context to an action.
#Context managerswith statement filehandling with open('file.ext') as f: contents = f.read() #Without context managers, this is what you have to do f = open('file.ext') try: contents = f.read() finally: f.close()
The above example is a particularly useful use for context managers and one that you will most likely be using a fair amount. Where you would usually have to call the file.close() method yourself once you are finished dealing with the file, using the with syntax it is already handled for you.
This was the last post in this series on common pythonic idioms. I hope you have learnt something new and found ways to improve your Python code, I know I have. Learning these idioms has really upped my code readability and even made it more fun to program in Python.
This isn’t the end though, there are many more quirks of the Python language to find and master, and many more being developed as you read this. It’s all about constantly learning.
‘Ello, I’m Jamal – a Tokyo-based, indie-hacking, FinTech software developer with a dependence on data.
I write Shakespeare-grade code, nowadays mostly in Python and JavaScript and productivity runs in my veins.
I’m friendly, so feel free to say hello!