Python objects

Author

Marie-Hélène Burle

In Python, everything is an object. So let’s talk about Python objects.

Fundamental definitions

Object

Every piece of data in Python, whether it’s a number, a string, a list, or a class instance is an object. Objects reside in memory and have a unique identity (address in memory), which can be checked using the id function. Objects have a type (e.g., int, str, list) which determines their behaviour and the operations that can be performed on them. Objects are the actual “things” that your program manipulates.

Value

A value is what an object stores. For example, an integer object might have the value 42, or a string object might have the value "hello". Two different objects can have the same value (e.g. two distinct integer objects both storing 5). The concept of “value” is often used when discussing the data itself, independent of its specific memory location or object identity.

Variable

In Python, a variable is a name that refers to an object. It acts as a label or reference to the specific memory location where an object is stored. When you assign a value to a variable, you are making that variable (name) point to an object holding that particular value.

Creating and deleting objects

Assignment

The assignment statement (=) assigns a variable (or name, or label, or reference) to an object in memory. This object holds a value.

For instance, we can assign the variable a to some object in memory that holds the value 1:

a = 1

You can define multiple variables at once, assigning them the same object:

a = b = 10
print(a, b)
10 10

… or different objects:

a, b = 1, 2
print(a, b)
1 2

Your turn:

a = 1
b = a
a = 2

What do you think the value of b is now?

The variable a gets assigned to an object holding the value 1. Then the variable b gets assigned to that same object in memory (you can double-check this with id(a) == id(b)). Finally, we reassign the variable a to a new object in memory holding the value 2. Meanwhile, the variable b still points to the first object with a value of 1 (so now id(a) == id(b) is not true anymore).

Choosing variables

While I am using a and b a lot in this workshop (since the code has no other purpose than to demo the language itself), in your scripts you should use meaningful names (e.g. survival, age, year, species, temperature). It will make reading the code this much easier.

Make sure not to use the names of built-in functions or built-in constants.

Deleting objects

You can delete variables with the del statement:

a = 3
print(a)
3
del a
print(a)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[5], line 2
      1 del a
----> 2 print(a)

NameError: name 'a' is not defined

Then the garbage collector automatically deletes from memory objects with no variables assigned to them.

Types

Python comes with multiple built-in types.

Examples (non exhaustive):

type(1), type(1.0), type('1'), type(3+2j), type(True), type(sum)
(int, float, str, complex, bool, builtin_function_or_method)

int = integer
float = floating point number
complex = complex number
str = string
bool = Boolean

Python is dynamically-typed, meaning you do not need to explicitly declare the type of a variable. It is inferred at runtime based on the value of the object the variable is assigned to.

Variables can be reassigned to objects holding different data types:

a = 2.3
type(a)
float
a = "A string."
type(a)
str

Type conversion

You can convert the type of some values. Here are some examples:

type('4'), type(int('4'))
(str, int)
type(3), type(str(3))
(int, str)
type(3), type(float(3))
(int, float)
type(3.4), type(str(3.4))
(float, str)
type(0), type(bool(0))
(int, bool)
type(True), type(int(True))
(bool, int)

Of course, not all conversions are possible:

int('red')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[15], line 1
----> 1 int('red')

ValueError: invalid literal for int() with base 10: 'red'

You might be surprised by some of the conversions:

int(3.9)
3
bool(3.4)
True

That’s because the Boolean of zero is False and the Boolean of any non-zero number is True.