Seven Sins of Python - Sin 7

Sin number 7: from nightmare import * I’ve been thinking long about which sin to name as the last in the grand seven of Python No-Nos. It finally came to me when working on one of legacy projects I’ve been contributing to (sadly this one is not open-source, so I cannot share the details). star imports -> imports formed like the titular from nightmare import *. Whence the temptation? Star imports are tempting because they seem to reduce boilerplate....

4 Jul 2022; Reading Time: 5 min

Seven Sins of Python - Sin 6

Sin number 6: Packaging not done right Packaging Python projects properly can be truly a pain in the neck. However learning how to properly set up a package is extremely valuable because adherence to standards means more people can use your package efficiently. 👉 The biggest sin of all things package-related is storing Python projects as simple collections of directories. Whence the temptation? Python is a simple scripting lanugage so the natural tendency newcomers have is to simply create folders with a bunch of scripts and then run those folders one by one....

17 Jan 2022; Reading Time: 11 min

Seven Sins of Python - Sin 5

Sin number 5: global destruction Every time I am asked about global keyword in Python, I get a split-second heart attack. Personally I think it is one of the most unnecessary and dangerous keywords that Python has to offer. I will go one step further and postulate that if you’re using nonlocal you’re probably doing it wrong. 👉 The essence of this problem is misunderstanding the purpose or the mechanics of scope in Python....

28 Dec 2021; Reading Time: 8 min

Seven Sins of Python - Sin 4

Sin number 4: Exception frenzy Python’s Exception class is probably one of the most abused and misused features of the language. It’s also something that no developer can escape from. Whence the temptation? An Exception is a pretty natural element of any language. In all languages one will always have some need for a runtime error. In case of Python, most runtime errors inherit from the Exception class. Though technically not a base class (for that would be BaseException), it should be treated like a base class and it often isn’t....

27 Dec 2021; Reading Time: 5 min

Seven Sins of Python - Sin 3

Sin number 3: Unconditional romance with ducktyping Whence the temptation? Python is truly awesome. In C++17 there is no easy way of expressing ranges. In Python an integer range \(\langle 0, 4 \rangle\) is simply range(0, 5). The syntactic simplicity and high conciseness of the language was originally related to the fact that Python does not have static typing. In Python ducktyping means that you can actually call a function that expects an integer with a string and you will likely only realize at runtime, since there is no compilation step....

17 Dec 2021; Reading Time: 5 min

Seven Sins of Python - Sin 2

Sin number 2: too few optimized functions used This one is pretty common especially for newcomers who have learnt enough about Python to be able to loop over data structures but haven’t yet realized how slow Python loops really are. Whence the temptation? If you’re fresh to Python, the temptation to create for loops everywhere for lists and dictionaries is immense. Consider a fairly simple example, where we loop over 100000 random integers and we apply a simple mathematical operation to each of those integers....

16 Dec 2021; Reading Time: 3 min

Seven Sins of Python - Sin 1

Sin number 1: Do not modify builtins and globals This is one of the biggest no-nos for me and something that I shot myself in the foot at least twice in my career as a Python dev. I’ve also seen this used in actual production code that was deployed to some important clients. Whence the temptation? builtins is a collection of objects that are available from within any Python script and are loaded before any script is executed by the interpreter....

10 Dec 2021; Reading Time: 3 min

Seven Sins of Python - Intro

Seven Sins of Python - intro Python is not only the most popular programming language (according to TIOBE as of 2021) but also one of the simplest to work with and easiest to learn. Python is great for a wide range of applications from web development through command line tools to large-scale automation and deep learning projects. It is far from the snappiest programming languages in terms of performance, however many other languages can be used alongside Python when performance is of essence....

10 Dec 2021; Reading Time: 2 min