5 things to love about python

Syntax

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.

Yield

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

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

Stop using await in a Python forloop Disclaimer: This might sound to be a rant but stay with me and you will see this will be a very good practical advise to speed up your async python c...
Solr Stats component is available in sunburnt StatsComponent is now available with stats function in the suburnt Solr python client library. More info: http://wiki.apache.org/solr/StatsComponen...
Python timeit – when speed matters – S... Although there are always multiple ways to solve a single problem not always the most elegant is the best performant. Python gives a perfect tool to c...