• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Data Dependence

Data Dependence

Learn about software development. Covering topics such as Coding in Python and JavaScript, developer productivity and more.

  • Home
  • Top Resources
  • Who Are You?
  • Show Search
Hide Search
You are here: Home / Programming / How to Write Beautiful Code with Pythonic Idioms | Notable Others

How to Write Beautiful Code with Pythonic Idioms | Notable Others

· · 2 Comments

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.

Don't forget to share and follow!

Remember, don't forget to share this post so that other people can see it too! Also, make sure you subscribe to this blog's mailing list and follow me on Twitter so that you don't miss out on any useful posts!

I read all comments, so if you have something to say, something to share or questions and the like, leave a comment below!

‘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!

Twitter is the best place for a chat.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Pocket (Opens in new window)

Related

Programming context managers, idioms, imports, naming conventions, programming, python, pythonic

Primary Sidebar

LEVEL UP with the Data Dependence Newsletter! Subscribe!


Top Posts & Pages

  • How to Find Unclosed Tags and Brackets Using a Stack
  • How to Build a GUI in Python 3.5
  • Mocking in Python - How to Bypass Expensive and External Code Within Your Tests
  • How to Logically Group Your Python Code into Modules
  • A Quick Guide to Slicing in Python - Become a Python Ninja

Data Dependence

Copyright © 2023 · Monochrome Pro on Genesis Framework · WordPress · Log in

  • Home
  • Top Resources
  • Who Are You?