Featured

Python Instances Explained: Data Types, Values, and Identity

In Python, every object you create is an instance with three key properties: its data type, its value, and its identity.

  • A data type is the blueprint that defines what values an instance can hold.
  • A value is the actual content stored in an instance.
  • An identity is the unique identifier that distinguishes one instance from another.

Understanding these concepts is essential for mastering how Python handles variables, memory, and equality checks. In this post, we’ll break down each property with examples you can try yourself.

 

An Instance with Its Three Properties

 

Python Data Types: Defining Instances

The data type serves as a blueprint when the instance is created and defines which values the instance may assume. For example, the int data type allows you to store an integer. Strings can be managed via the str data type. In the following example, you can see how to identify the data types of different instances using type:

 

>>> type(1337)

<class 'int'>

>>> type("Hello World")

<class 'str'>

>>> v1 = 2674

>>> type(v1)

<class 'int'>

 

The type function is useful, among other things, when you want to check whether two instances have the same type or whether an instance has a specific type:

 

>>> v1 = 1337

>>> type(v1) == type(2674)

True

>>> type(v1) == int

True

>>> type(v1) == str

False

 

Note, however, that a type refers only to instances and has nothing to do with the linked references. A reference has no type and can reference instances of any type. The following is absolutely possible:

 

>>> first_a_string = "I am a string"

>>> type(first_a_string)

<class 'str'>

>>> first_a_string = 1789

>>> type(first_a_string)

<class 'int'>

 

So it's wrong to say "first_a_string has type str." The correct thing to say is "first_a_ string currently references an instance of type str." However, since the former leads in many cases to much simpler and more comprehensible descriptions, this formulation is also often used in the context of Python. In this book we also simplify many descriptions in the interest of the readability in this way.

 

Python Instance Values: How Equality Works

What specifically constitutes the value of an instance depends on its type. Types can be, for example, numbers, strings, or data of other types, which you’ll get to know later. In the preceding examples, they were 1337, 2674, 1798, "Hello world", and "I am a string".

 

You can use the == operator to compare instances with respect to their value:

 

>>> v1 = 1337

>>> v2 = 1337

>>> v1 == v2

True

>>> v1 == 2674

False

 

With the help of our graphical model (see the figure below), the operation of the == operator can be well illustrated.

 

Value Comparison of Two Instances (in This Case, False)

 

The value comparison is only useful if it refers to structurally similar data types, such as integers and floats:

 

>>> number1 = 1987.0

>>> type(number1)

<class 'float'>

>>> number2 = 1987

>>> type(number2)

<class 'int'>

>>> number1 == number2

True

 

Although number1 and number2 have different types, comparing them via == returns the truth value True.

 

Structurally, numbers and strings have little in common as numbers are single values, while strings combine several letters into a single unit. For this reason, the == operator for the comparison between strings and numbers always returns False, even if the values look "the same" to a human:

 

>>> string = "1234"

>>> string == 1234

False

 

Whether the == operator is defined for two particular types depends on the data types themselves. If it isn’t present, the identity of the instances is used for comparison, which is explained in the following section.

 

Python Identity: Understanding the id() Function

The identity of an instance serves to distinguish it from all other instances. This can be compared to a person's individual fingerprint as it’s unique for each instance within a program and cannot change. An identity is an integer and can be determined using the id function:

 

>>> id(1337)

134537016

>>> v1 = "Hello World"

>>> id(v1)

3082572528

 

Identities become important whenever you want to check if an instance is specific and not just one with the same type and value:

 

>>> v1 = [1,2,3]

>>> v2 = v1

>>> v3 = [1,2,3]

>>> type(v1) == type(v3)

True

>>> v1 == v3

True

>>> id(v1) == id(v3)

False

>>> id(v1) == id(v2)

True

 

In this example, Python has created two different instances with type list and value[1,2,3], where v1 and v2 reference the same instance. This figure illustrates this graphically.

 

Three References, Two Instances

 

The comparison on identity equality is so important in Python that a separate operator has been defined for this purpose: is.

 

The expression id(reference1) == id(reference2) means the same as reference1 is reference2. The figure below illustrates this.

 

Identity Comparison of Two Instances

 

The comparison shown above yields the truth value False because the identities of the two instances differ.

 

Editor’s note: This post has been adapted from a section of the book Python 3: The Comprehensive Guide by Johannes Ernesti and Peter Kaiser. Johannes is a research scientist at DeepL. He is a graduate mathematician and received his doctorate in applied mathematics from the Karlsruhe Institute of Technology (KIT). Peter is a research scientist at DeepL. He has a degree in computer science and received a doctorate in humanoid robotics from the Karlsruhe Institute of Technology (KIT).

 

This post was originally published 9/2025.

Recommendation

Python 3
Python 3

Ready to master Python? Learn to write effective code with this award-winning comprehensive guide, whether you’re a beginner or a professional programmer. Review core Python concepts, including functions, modularization, and object orientation, and walk through the available data types. Then dive into more advanced topics, such as using Django and working with GUIs. With plenty of code examples throughout, this hands-on reference guide has everything you need to become proficient in Python!

Learn More
Rheinwerk Computing
by Rheinwerk Computing

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes books by leading experts in the fields of programming, administration, security, analytics, and more.

Comments