Enums in Python

Posted on Fri, 17 Feb 2017 in Python • Tagged with enums, python

Enums are less used feature in Python. We as programmers prefer to use weird dicts or lists where enum should be used. Why? Because it is rather new feature and you have to install back port library if you use Python 2.7. However, it's better to use them in many cases.

Continue reading

Java-like Optional in Python

Posted on Wed, 23 Nov 2016 in Python • Tagged with python, java

Sometimes using None makes a mess of a code. For example, if you want to separate some "empty" value and "no data" value for integer input where 0 isn't "empty" value, it's very hard to do with None only. Possible solution is to use something like Optional in Java.

Continue reading

Backtracing without Recusion in Python

Posted on Thu, 03 Nov 2016 in Python • Tagged with python, backtracking, recursion

As you know there is no tail recursion optimization in Python. Moreover it has a very low recursion limit. It can be a problem if you are trying to solve the problem using the backtracking algorithm. Recursion limit is big enough to solve sudoku, but an exception will raise if the problem has more possible solutions.

Continue reading

Want to add type annotation to a variable? Do refactoring!

Posted on Thu, 06 Oct 2016 in Python • Tagged with python, type hinting

There is the PEP 526 implemented in Python 3.6 that adds optional type annotations for variables. It's replacement for type comments. There is one pitfall: adding type annotations for local variables can hide problems with code.

Continue reading

Don't use dicts so much

Posted on Fri, 09 Sep 2016 in Python • Tagged with python, dict, rest

Python developers tend to use dicts too much. Mainly I tell about pass data through application as dicts not objects. It's a bad design witch causes many problems.

Continue reading

How to use mypy on Python 2.7 project

Posted on Fri, 29 Jul 2016 in Python • Tagged with python, mypy, type hinting

If you work on a big Python project, type hinting can help you to find many design and function usage problems. Of course, you have to use external tools for static analysis, such as mypy. This is a great tool, but supports Python 3 mainly. Support for 2.7 branch isn't so straightforward. There are a couple of tricks that allows you to use it for 2.7.

Continue reading

PyCon Russia 2016

Posted on Thu, 07 Jul 2016 in Python • Tagged with python, conference, pycon

PyConRu 2016 finished. It was the best PyConRu conference. Usually If there are two useful talks, conference is good. This time I found useful many amazing talks enough to fill an entire day-long session.

Continue reading

Build own iterator over enumerate

Posted on Mon, 27 Jun 2016 in Python • Tagged with python, iterators

Couple days ago one of my colleagues asked me to implement simple self-logging iterator inherited from enumerate. I tried to inherit directly from it and failed. I absolutely forgot how to use __new__ magic method. It was so embarrassing. I was in a hurry, so I promised myself to solve this puzzle. And I can tell you it is a simple task. Actually it takes 18 lines of code only.

Continue reading

My top 7 of PyCon 2016 videos

Posted on Tue, 14 Jun 2016 in Python • Tagged with pycon, python, conference

There is my list with 7 best PyCon 2016 videos. I don't say that other videos not good. I say that these videos are more useful for me or impress me more than others. And as you can see I prefer talks about Python internals. I hope you'll find this list helpful.

Continue reading

Mutable types as default params in Python

Posted on Thu, 09 Jun 2016 in Python • Tagged with Python, default params

Do you ask yourself why you souldn't use mutable types as default value for parameters in Python? I'm sure you ask at least if you are not a junior. Probably, your answer is "It causes to strange side effects". However, only a tiny part of python developers makes step throw and figure out how it works under the hood.

Continue reading