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
Month: July 2014
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.
Python bytecode
Just like Java or C#, CPython is compiling the code into bytecode which is then interpreted by a virtual machine. The Python library dis allows to disassemble Python code and to see how are things are compiled under the hood. Consider the following code:
>>> def test(): ... for i in range(10): ... print(i) ...
You can call dis.dis(test) to display the compiled bytecode, and dis.show_code(test) to understand the symbols referenced by that bytecode. Continue reading
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