Michael Mentele

software

Python Language Fundamentals

When learning a new language there a few things that are important to understand about the tradeoffs the language designer has made which imply the usecases the language is appropriate for like:

  • execution model
  • data model
  • scoping
  • memory management

Execution Model

Python is an interpreted language, meaning that the syntax is ‘interpreted’ into byte code at runtime instead of pre-compiled like Java or C. As you probably know, the tradeoff of any interpreted languages is that it shift burden away from the programmer and onto the interpreter/compiler (ie. you don’t need to statically type variables). This means that Python has to figure out what the type of a variable is when it is reading or interpreting your code whereas in Jave or C you declare it. At the end of the day the so-called dynamically typed variables will assigned some memory in accordance with their data type so at the machine level you could say everything is statically typed. Note that Python also is strongly typed, meaning it does not implicitly coerce variable types (ie. 1 + ‘1’ will return an error) this is in contrast to Javascript and Ruby wherein both languages will coerce ‘1’ to 1.

In Python, variables aren’t really containers of values. Instead they are a name that binds to a given object. This is pretty intuitive if you know what a pointer is.

Data Model

In Python everything is an object meaning types have methods, functions have attributes. Objects are either mutable or immutable following their creation.

All objects have an identity, type, and value where only a value can be changed. An identity is an address in memory and a type is the class of the object. In general, objects are not destroyed though they are garbage collected when they are unreachable.

Interestingly, although the names (addresses) that are bound (pointed to) can be frozen, for example within a tuple (immutable list), you can modify the objects that are pointed to and therefore indirectly modify what is ‘contained’ by the tuple.

Scope

Scope is lexical in Python in a similar manner to Javascript. A suite (AKA block) in Python is the unit of scope. The three major blocks are: modules, classes, and functions.

Lexical scoping means that scope is nested, I imagine this as a terrace or quary where the depth (number of levels) each level has access to the ones above. Though it may make more sense to think of this in reverse. The point is the deeper the nesting, the more scope it has access to.

class OuterScope():
    x = 1
    def inner_scope():
       def inner_inner_scope():
          x = 2
          print(x)
          # x => 2

        print(x) # In Ruby, this would throw an error; x is not defined
        # x => 1

        x = 3
        print(x)
        # x => 3

    print(x)
    # x => 1

When a name in an inner scope shadows a name above it, it does not overwrite that name however in the local scope of the current block that value is used.

There are two ways you can modify scope. The global and nonlocal keywords. These should probably be avoided, but essentially you can make a variable/name available to everything within the scope of the current module at any point in the code. With nonlocal if you set a variable in a deeper level it will clobber a variable of the same name in the enclosing scope.

Memory Management

Python uses a private heap to store all of its objects. Various primitives have their own special memory management as an integer has different memory needs than a dynamically sized list. Objects in this private heap are garbage collected using generational garbage collecting, which though it has a higher overhead than mark and sweep, it also doesn’t pause the world when it needs to run, instead it incurs a slightly higher total cost that is amortized accross the programs execution lifecycle.

Python has support for threading but each thread must be passed a GIL (Global Interpreting Lock) so the thread can modify shared memory.

Closing

These are useful high level topics that should be covered when learning a new language. For complete newcomers it is best to start with syntax, but once you’ve mastered a language learning syntax is trivial, what is more interesting is the fundamental design of a language and what tradeoffs it has made.

Beyond that is the programming model, what are the idioms of the language, the conceptual design? How does one think about the world through the lens of their language? I may talk about this in a follow up article.