As everything in Python is an object, classes are created by instanciating a super class called a metaclass. Metaprogramming is about overriding some behavior of that metaclass. This is often used to follow the DRY principle (Don’t Repeat Yourself) in order to avoid repeating the same or similar code inside a program. Metaprogramming is thus often used by frameworks such as Django as it helps to make their API much easier to use.
Continue reading
Python
Collections
Python, like languages, contains several built-in collections. Let’s review the various types of collection and how can you also create your own collections.
Continue reading
Object comparison
There are things that vary greatly from language to language. After variable scope, let’s look at object comparison. That is, how are objects compared to each other? How are they evaluated as a condition?
Continue reading
Dynamic typing (part 1)
Python is a dynamically-typed language, just like PHP, JavaScript or Ruby, and contrary to statically-typed languages such as C/C++, Java or Scala.
In a dynamically-typed language, the code can never know in advance what will be the type of any variable. You can define a function to perform a particular operation on, say, a collection, but unless you explicitly filter out an argument that is not a collection, the code is never certain that it is indeed one – and the bytecode compiler sure cannot be certain either.
Using dynamic types has consequences for the language – both from a conceptual standpoint and from an implementation standpoint.
Continue reading
Everything’s an object
In Python, pretty much everything is an object, whether it is a number, a function… or even None.
Continue reading
The Garbage Collector
Python, like most modern languages, has its own garbage collector (GC). But how does the CPython GC work?
First of all, does it really matter? After, a GC is a GC, right? Well, not exactly. The GC on Java has evolved quite a bit from the days of Java 1.0 (you can find a great deal information about the various algorithms used here). In a interview, three Twitter developers explained the move from Ruby to Scala (a language that runs on top of the Java VM). And one of the reasons was that “because Ruby’s garbage collector is not quite as good as Java’s, each process uses up a lot of memory“. So yes, the GC can matter.
Continue reading
On immutability
An immutable structure is a structure that does not change once it has been created. If the concept is nothing new, it has been used more and more for its upsides. Let’s review how Python handles immutability.
Continue reading
Variable scope
One of the important thing to know when learning a programming language is the variable scope – in other words, what variable can be seen where. It can indeed greatly vary from language to language.
Even though we always talk of variables, Python does not really have variables in the traditional sense (see Variables and integers). Rather, it has names in namespaces. In the current namespace a certain number of names are available. You can use dir() to see what is in the current namespace.
Variables and integers
Python does not have variables like in other languages. In a language such as C++ or Java, writing something like:
int a = 42;
means that some memory gets allocated to store an integer value and that a variable gets associated with that memory range. When you change the value of the variable a, the memory gets modified.
In Python, things work differently. Continue reading