Data Type Introduction¶
Indices and tables¶
Python comes with a rich set of stock data types, known as the Standard Type Hierarchy, and shown in Fig. 22:
Many of the data types in Fig. 22 will be familiar to you from other programming languages. Although Enums are not part of the Standard Type Hierarchy, they are part of the Standard Library and will be covered in this course.
What may be new is that Python treats first class constructs (e.g. user-defined functions and the built-in functions) as types, where other languages do not. For example, try getting the type of the dir()
method:
>>> type(dir)
<class 'builtin_function_or_method'>
Which shows it is an instance of the builtin_function_or_method
class. Let’s see what the type of that class is:
>>> type(type(dir))
<class 'type'>
Which shows that the builtin_function_or_method
class is itself an instance of the class called type
. Rather interesting is the fact that the type
class is also an instance of the type
:
>>> type(type(type(dir)))
<class 'type'>
So, type is a type is a type…
And by now, if the repeated use of the word “type” has given you semantic satiation, you are not alone.
Anyway, don’t let this create a black hole in your brain because the only salient point to take away is that this is just confirmation that everything in Python is an object!
Not all of the data types in Fig. 22 need to be explored and understood before you can effectively use Python. This course will cover only the ones that will give you the most bang for the buck up front. But before we get to that, Python has the concept of immutable and mutable types that needs to be understood first.
Immutable: the value of the object cannot change after creation. Python hides this detail from you most of the time by creating a new object if you attempt to change the value of an immutable object. For example:
Creating a variable, in this case
a
, with a value of 1 creates an integer object in memory:>>> a = 1 >>> a 1 >>> id(a) 140295909546656
We can have other variables (remember these are actually names), point to the same integer object in memory, and using the
id()
function we can see they are indeed pointing to the same object:>>> b = a >>> b 1 >>> id(b) 140295909546656
If we try and change the value of
a
, integers are immutable so Python creates a new integer object in memory with a value of 2 and sets the namea
to point to it:>>> a = 2 >>> a 2 >>> id(a) 140295909546688
However, the name
b
still points to the original integer object:>>> b 1 >>> id(b) 140295909546656
What this ultimately means is that if you try and change the value of an immutable object, only the name you accessed to change the value, sees the new value. Any other names that refer to the immutable object still see the old object value.
Mutable: the value of the object can change after creation. This is probably the behavior most people are used to dealing with. Let’s take a look at what this means.
In Python, the
list
class (an array) is mutable. Creating a variable, in this casea
, with an initial array value of [1, 2] creates a list object in memory:>>> a = [1, 2] >>> a [1, 2] >>> id(a) 140295890249288
Similar to above, We can have other variables point to the same list object in memory, and we can confirm they are indeed pointing to the same object:
>>> b = a >>> b [1, 2] >>> id(b) 140295890249288
With mutable objects, Python does not create a new object instance when we modify the value of the mutable object.
>>> a[1] = 5 >>> a [1, 5] >>> id(a) 140295890249288
Because a new instance was not created,
a
andb
still point to the same list object:>>> b [1,5] >>> id(b) 140295890249288
What this ultimately means is that if you try and change the value of an mutable object, all names referring to that object will see the change in value.