5 things to love about python


Takes a bit of time to get used to it but after a certain point it just becomes natural. Like english. No heavy-weight operators or complicated syntax. I can bring a lot of examples but this is not about specific commands this is about the soul of the language.


I think this is one of the most overlooked function in python. I see so few occurrence and I’m very surprised about this. So when does this really come handy. If you want to write clean code you put different logic in different layers of code which you eventually want to represent. Let’s say we have an MVC framework (Django will do) where you want some logic on the models to happen on the in the model layer and you want to rearrange the result set of a query in a dictionary in the view and represent it in the template. You need three for cycle for this. You go through the list three times unnecessary as all you have to do is instead of return do a yield in the model and controller. Small tuning steps like this can make huge different in the long run.

Instead of

You can write this:


Decorators are one of the first subjects pop up when somebody talks about python so I don’t think I need to write novels about how helpful are they. I just simply love their existence and use them in my every day tasks a lot.

Inline statements

Usually it’s a bad practice to try to enforce everything in one line but there are cases when is extremely helpful not to write complex code blocks. I think real life examples tell more than description.

Set the modules for a client based on its subscription bundle.

Prepare a days dictionary for storing opening hours of a shop.

Distance form field in django with configured choices.

Everything is an object

I saw first time pythoners to have difficulties understand the concept of everything being an object however it comes very handy in many times. Again let’s see an example what I made as a nagios check_nrpe command:

This is a decorator which can have parameters at initialization. What it does is converting a simple function which returns a number and a message to a check_nrpe command.

The reason why this can work relies in the fact that everything is an object. Functions are objects, classes are objects. When the decorator is added to the latest_backup function what actually happens is the check_numeric class got initialized with critical and warning parameters and return an object which has the __call__ method defined to modify the behavior of the fn function in the parameter list.

You might like these too

Sunburnt Solr spatial filter support I've added spatial filtering to the sunburnt library. The feature is now available with filter_spatial function. Example usage Currently this will...
Python MySQLdb vs mysql-connector query performanc... There are a lot of python driver available for MySQL and two stand out the most. The one, traditionally everybody's choice, sort of industrial standar...
Solr python interface – Sunburnt fork I forked tow (Toby White, gratitude for his great work) sunburnt repository from github because I needed range facet funcionality and the workaround (...

About charlesnagy

I'm out of many things mostly automation expert, database specialist, system engineer and software architect with passion towards data, searching it, analyze it, learn from it. I learn by experimenting and this blog is a result of these experiments and some other random thought I have time to time.
Bookmark the permalink.