6. OOP I: Objects and Methods#

6.1. Overview#

The traditional programming paradigm (think Fortran, C, MATLAB, etc.) is called procedural.

It works as follows

  • The program has a state corresponding to the values of its variables.

  • Functions are called to act on and transform the state.

  • Final outputs are produced via a sequence of function calls.

Two other important paradigms are object-oriented programming (OOP) and functional programming.

In the OOP paradigm, data and functions are bundled together into “objects” — and functions in this context are referred to as methods.

Methods are called on to transform the data contained in the object.

  • Think of a Python list that contains data and has methods such as append() and pop() that transform the data.

Functional programming languages are built on the idea of composing functions.

So which of these categories does Python fit into?

Actually Python is a pragmatic language that blends object-oriented, functional and procedural styles, rather than taking a purist approach.

On one hand, this allows Python and its users to cherry pick nice aspects of different paradigms.

On the other hand, the lack of purity might at times lead to some confusion.

Fortunately this confusion is minimized if you understand that, at a foundational level, Python is object-oriented.

By this we mean that, in Python, everything is an object.

In this lecture, we explain what that statement means and why it matters.

We’ll make use of the following third party library

!pip install rich
Requirement already satisfied: rich in /usr/share/miniconda3/envs/quantecon/lib/python3.11/site-packages (13.3.5)
Requirement already satisfied: markdown-it-py<3.0.0,>=2.2.0 in /usr/share/miniconda3/envs/quantecon/lib/python3.11/site-packages (from rich) (2.2.0)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/share/miniconda3/envs/quantecon/lib/python3.11/site-packages (from rich) (2.15.1)
Requirement already satisfied: mdurl~=0.1 in /usr/share/miniconda3/envs/quantecon/lib/python3.11/site-packages (from markdown-it-py<3.0.0,>=2.2.0->rich) (0.1.0)

6.2. Objects#

In Python, an object is a collection of data and instructions held in computer memory that consists of

  1. a type

  2. a unique identity

  3. data (i.e., content)

  4. methods

These concepts are defined and discussed sequentially below.

6.2.1. Type#

Python provides for different types of objects, to accommodate different categories of data.

For example

s = 'This is a string'
x = 42   # Now let's create an integer

The type of an object matters for many expressions.

For example, the addition operator between two strings means concatenation

'300' + 'cc'

On the other hand, between two numbers it means ordinary addition

300 + 400

Consider the following expression

'300' + 400
TypeError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 '300' + 400

TypeError: can only concatenate str (not "int") to str

Here we are mixing types, and it’s unclear to Python whether the user wants to

  • convert '300' to an integer and then add it to 400, or

  • convert 400 to string and then concatenate it with '300'

Some languages might try to guess but Python is strongly typed

  • Type is important, and implicit type conversion is rare.

  • Python will respond instead by raising a TypeError.

To avoid the error, you need to clarify by changing the relevant type.

For example,

int('300') + 400   # To add as numbers, change the string to an integer

6.2.2. Identity#

In Python, each object has a unique identifier, which helps Python (and us) keep track of the object.

The identity of an object can be obtained via the id() function

y = 2.5
z = 2.5

In this example, y and z happen to have the same value (i.e., 2.5), but they are not the same object.

The identity of an object is in fact just the address of the object in memory.

6.2.3. Object Content: Data and Attributes#

If we set x = 42 then we create an object of type int that contains the data 42.

In fact, it contains more, as the following example shows

x = 42

When Python creates this integer object, it stores with it various auxiliary information, such as the imaginary part, and the type.

Any name following a dot is called an attribute of the object to the left of the dot.

  • e.g.,imag and __class__ are attributes of x.

We see from this example that objects have attributes that contain auxiliary information.

They also have attributes that act like functions, called methods.

These attributes are important, so let’s discuss them in-depth.

6.2.4. Methods#

Methods are functions that are bundled with objects.

Formally, methods are attributes of objects that are callable – i.e., attributes that can be called as functions

x = ['foo', 'bar']

Methods typically act on the data contained in the object they belong to, or combine that data with other data

x = ['a', 'b']
s = 'This is a string'
'this is a string'
s.replace('This', 'That')
'That is a string'

A great deal of Python functionality is organized around method calls.

For example, consider the following piece of code

x = ['a', 'b']
x[0] = 'aa'  # Item assignment using square bracket notation
['aa', 'b']

It doesn’t look like there are any methods used here, but in fact the square bracket assignment notation is just a convenient interface to a method call.

What actually happens is that Python calls the __setitem__ method, as follows

x = ['a', 'b']
x.__setitem__(0, 'aa')  # Equivalent to x[0] = 'aa'
['aa', 'b']

(If you wanted to you could modify the __setitem__ method, so that square bracket assignment does something totally different)

6.3. Inspection Using Rich#

There’s a nice package called rich that helps us view the contents of an object.

For example,

from rich import inspect
x = 10
╭────── <class 'int'> ───────╮
 int([x]) -> integer        
 int(x, base=10) -> integer 
 denominator = 1            
        imag = 0            
   numerator = 10           
        real = 10           

If we want to see the methods as well, we can use

inspect(10, methods=True)
╭───────────────────────────────────────────────── <class 'int'> ─────────────────────────────────────────────────╮
 int([x]) -> integer                                                                                             
 int(x, base=10) -> integer                                                                                      
      denominator = 1                                                                                            
             imag = 0                                                                                            
        numerator = 10                                                                                           
             real = 10                                                                                           
 as_integer_ratio = def as_integer_ratio(): Return integer ratio.                                                
        bit_count = def bit_count(): Number of ones in the binary representation of the absolute value of self.  
       bit_length = def bit_length(): Number of bits necessary to represent self in binary.                      
        conjugate = def conjugate(...) Returns self, the complex conjugate of any int.                           
       from_bytes = def from_bytes(bytes, byteorder='big', *, signed=False): Return the integer represented by   
                    the given array of bytes.                                                                    
         to_bytes = def to_bytes(length=1, byteorder='big', *, signed=False): Return an array of bytes           
                    representing an integer.                                                                     

In fact there are still more methods, as you can see if you execute inspect(10, all=True).

6.4. A Little Mystery#

In this lecture we claimed that Python is, at heart, an object oriented language.

But here’s an example that looks more procedural.

x = ['a', 'b']
m = len(x)

If Python is object oriented, why don’t we use x.len()?

The answer is related to the fact that Python aims for readability and consistent style.

In Python, it is common for users to build custom objects — we discuss how to do this later.

It’s quite common for users to add methods to their that measure the length of the object, suitably defined.

When naming such a method, natural choices are len() and length().

If some users choose len() and others choose length(), then the style will be inconsistent and harder to remember.

To avoid this, the creator of Python chose to add len() as a built-in function, to help emphasize that len() is the convention.

Now, having said all of this, Python is still object oriented under the hood.

In fact, the list x discussed above has a method called __len__().

All that the function len() does is call this method.

In other words, the following code is equivalent:

x = ['a', 'b']


x = ['a', 'b']

6.5. Summary#

The message in this lecture is clear:

  • In Python, everything in memory is treated as an object.

This includes not just lists, strings, etc., but also less obvious things, such as

  • functions (once they have been read into memory)

  • modules (ditto)

  • files opened for reading or writing

  • integers, etc.

Remember that everything is an object will help you interact with your programs and write clear Pythonic code.

6.6. Exercises#

Exercise 6.1

We have met the boolean data type previously.

Using what we have learnt in this lecture, print a list of methods of the boolean object True.