If you are reading this, I bet there’s a good chance that you have used Flask or Django at some point in your life. My feelings towards Flask are perfectly described by this article and I think that we all can agree that global objects is something to be avoided at all costs. In the article the author proposes Django as a superior alternative and I personally, if forced to choose between the two, would go with the latter. However, I don’t think Django is an example of a good framework design either.
Here are some code examples take a look at the official tutorial helping us write our first app with Django:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
OK, so the problems start right here. Do you really know how this code works? We just leave this variable
urlpatterns right here and we suppose that some code, which knows where to look, will look into this file and extract this variable.
Another example with the simplest view handler function:
from django.http import HttpResponse
return HttpResponse("Hello, world. You're at the polls index.")
If we take a look inside
HttpResponse we will find that it is a simple DTO. Who invokes the request? Where does the response object go? Where’s this magical code running everything?
Is it easy for us to start at some point in this code and click through it (I mean pressing
Ctrl + LMBon objects in PyCharm) deeper and deeper reaching down to the WSGI-level code in Django and actually see how our own code is called? In other words, is it easy for us to trace the flow of data from the point where a request is entering our WSGI to the point where a response is sent?
In fact, we could start from the
wsgi.py file in our core app. What we will see under the hood is pretty sophisticated and hard to read code using a lot of global objects (I encourage you to take a look at
BaseHandler and whatever you come across inside
Regarding the source code we actually face during our attempts to get to the core: I would say, although it is not as bad as Flasks’s 2000 lines of code in a single class, it is still hard to grasp the overall picture of what the heck it is doing and why.
So here’s my point: if you are using some tool, especially if you are using it more than once or twice in you life, it is very useful to have a pretty good understanding how it works. Having a good understanding of how your tool works is even better when it comes to extending your tool, hacking on it or fixing some bug, — that’s pretty much the reason why soldiers are taught to disassemble and assemble their rifles. There’s a difference between looking simple and actually being simple and unfortunately, most of the Python libraries today are concerned only about the former.