Python – Dynamic Typing.

This post is the seventh of many that attempts to document everything I have been learning about Python.

Much of Python’s conciseness, flexibility and power comes from the fact that it is a dynamically typed language.

Dynamic typing means that you, the developer, do not have to declare variables ahead of time and you do not have to specify the type of the variable.

Variables do not have types, they are just pointers to objects in memory.

Objects have types, variables only point to objects.

For example, when you run this line of code:

>>> desc = “Python Programmer

The string “Python Programmer” is assigned to the variable desc.

The following steps have to happen for this work:

1. Python creates a object in memory to hold the string “Python programmer”
2. It creates the variable desc.
3. It assigns the pointer (the address of the location where the string is being stored) to the variable desc.

Because all that a variable holds is the address to an object, you can re-assign that variable to point to other objects during program execution.

In the example below, the variable desc first points to a string and is then re-assigned to point to a int and then a list.

>>> desc = “Python Programmer
>>> desc = 1
>>> desc = [1, 2, 3]

This is very different from static, compiled languages like C# and Java, where the variable has a type associated with it and it can only hold values of the same type.

In Python:

Variables have to be assigned before they can be used.

Variables only point to objects and not to each other.

When Python creates an object in memory, the object has 2 properties associated with it, its type name, and a reference count.

1. The type name is actually the reference to the type’s class (int for integers, str for strings..)
2. The reference count is a counter that stores the number of references that are pointing to the object.

References to immutable objects.

>>> var1 = “The Adam Carolla Podcast
>>> var2 = var1
>>> var2 = “Loveline

When you run the first 2 lines of code, a object (string) is created in memory to hold the string. The address of that memory location is then stored in var1.

In the second line, that address is copied over to var2.

Now var1 and var2, both point to the same memory location.

In the 3rd line, you create a new object in memory that holds the new string and store its memory location in var2.

var1 and var2 now point to 2 different locations in memory.

References to mutable objects

>>> comedy_podcasts = [“Adam Carolla“, “Dave Dameshek“, “Rep. Richard Martin“]
>>> podcasts = comedy_podcasts
>>> comedy_podcasts, podcasts
([‘Adam Carolla‘, ‘Dave Dameshek‘, ‘Rep. Richard Martin‘], [‘Adam Carolla‘, ‘Dave Dameshek‘, ‘Rep. Richard Martin‘])
>>> comedy_podcasts[1] = “Dave Feeney
>>> comedy_podcasts
[‘Adam Carolla‘, ‘Dave Feeney‘, ‘Rep. Richard Martin‘]
>>> podcasts
[‘Adam Carolla‘, ‘Dave Feeney‘, ‘Rep. Richard Martin‘]

In this scenario, you create a mutable type (list) in memory and store its memory location in variable comedy_podcasts.

You then assign that same memory location to variable podcasts.

In the 3rd line, when you modify comedy_podcasts, the list is being changed ‘in place’. This is because lists are a mutable type and can be changed in place. So what is really happening is that in line 3, you are actually changing the object itself.

Since podcasts is pointing to the same object in memory, its value has also been affected.

If you want to assign the copy of an object instead of its reference, here is some ways of doing it.

For lists, the simplest way is to slice it, this will create another list in memory and assign its address to the new variable.

>>> list1 = [1,2,3]
>>> list2 = list1[:]
>>> list1[0] = “change
>>> list1
[‘change‘, 2, 3]
>>> list2
[1, 2, 3]

Now only list1 is affected, list2 points to a new list in memory.

You can import the copy module and use its copy and deepcopy methods for objects to have this same effect.

Garbage Collection

When an object is created in memory, it has 2 header fields. One holds the type of the object, the other holds a count of references to the object.

When a object’s reference counter hits 0, that is to say, no variable is referring to it, the object is claimed by the garbage collector.

Some immutable objects might be cached by the Python execution environment for future use, but this should not be of any concern to the developer.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s