Notes on Python


Python is often referred as "scripting language", for its simplicity and readability, but it is also a powerful programming language due to its features, such as [1]
  • Dynamic typing (no need to declare variable type but to assign a value in its declaration);
  • Automatic memory management (garbage collector);
  • Large system building support (modules, classes, exceptions).
  • Procedural [2], object oriented [3] and functional [4] programming paradigms. 
  • Integration with other languages 
This makes Python great for simple and small tasks to big control systems, specially when it comes to code maintenance. Even though the performance still is a drawback, this won't be noticed unless the runtime speed is crucial to your work [1]. 

The performance drop is because Python is interpreted, not compiled. Although the interpreter do compile a translated version of your ".py" file to a byte code (that is not binary machine code) to run faster, there is also a virtual machine to actually execute it [1].

Why Python sacrifices performance? Because of dynamic typing. In general, this kind of languages tend to need less code lines and are more readable [5], and that's one of Python's principles. Even though an interpreted language is not always dynamic typed, in many cases they walk side by side [6]. 

Being a dynamic typed language means that the variables must be initialized to define its type, in contrast to static typed languages as C++, where this is not necessary [7]. The language itself keeps track of the types automatically [1]. A better way to memorize this, is associating dynamic typing to variable type check at runtime and static typing to variable type check at compile time, although this is not the best description of what they are.

There is also another classification: strong and weak typing. A strong typed language as Python bounds its variable to a data type, so you can't add a number to a string, for instance [7], it will produce a type error. Don't mistake this with initializing a variable as a number and then assigning the same variable to a string. This has to do with a set of operations that are only valid for a type [1]. On the other hand, a weak typed language, as PHP and JavaScript, do the opposite, and can implicitly guess that a "sum" of a string and an integer is actually a string concatenation.

Another feature of Python is its object types. Everything in Python is an object, from number to functions [1]. They are the basic structure of a conceptual hierarchy [1]:
  • Programs are composed of modules ("py" files)
  • Modules contain statements (flow control, assignment, calls).
  • Statements contain expressions (what manages the objects).
  • Expressions create and process objects (numbers, strings, lists, sets, files).
With built-in objects, there is no need to worry about memory management as happens in C and C++, where you write your own data structure.



Knowing the structure and the typing of a language brings to attention some mistakes that may pass unnoticed, that is, the compiler/interpreter won't warn you. This is a topic for later on. For now, let's move on to the Python's versions differences that also can lead to some mistakes. 

[2] List of steps with possible use of control flow statements (if, else, for) represented by a block of code within a scope of a module.
[3] Way of modeling a system where you can define an object (class instance) as responsible to accomplish a task through its methods that manipulates its own data.
[4] Form of declarative programming that primarily avoids shared state, as what happens when two methods manipulate the same data.

No comments:

Post a Comment

Global Game Jam 2024

Wow, what a weekend. The Global Game Jam is intense. But I and 3 other members manage to build a game for the theme "Make me laugh&quo...