Sitemap
Python Land

Python articles for all types of programmers, from beginner to expert, from DevOps to Data Scientists

30 Python Language Tricks That Will Make You a Better Coder

If you think you mastered the language, you’ve got to read this

11 min readJan 26, 2021

--

Press enter or click to view image in full size
Image
Image by author

There are lots of little tricks that can make life easier for a Python coder. Some of these will be known to you, but I’m sure there are at least a couple that you haven’t seen before.

If you’re done reading, also take a look at our latest article on concurrency:

1. The Python Ellipsis

Image

The Python ellipsis is a sequence of three dots. It’s used a lot in conventional (non-programming) languages. But what you might not know, is that it’s a valid object in Python too:

>>> ...
Ellipsis

Its primary use seems to be in matrix slicing operations in NumPy. However, you can use it as a placeholder in a function that you haven’t implemented yet, instead of using pass, as most people do:

def my_awesome_func():
...

This is valid Python code, and it doesn’t look too bad now, does it?

2. Data classes

Since version 3.7, Python offers data classes. There are several advantages over regular classes or other alternatives like returning multiple values or dictionaries:

  • a data class requires a minimal amount of code
  • you can compare data classes because __eq__ is implemented for you
  • you can easily print a data class for debugging because __repr__ is implemented as well
  • data classes require type hints, reduced the chances of bugs

Here’s an example of a data class at work:

--

--

Python Land
Python Land

Published in Python Land

Python articles for all types of programmers, from beginner to expert, from DevOps to Data Scientists

Responses (11)