Objects — To be (mutable) or not to be (mutable) — that is the question
At Holberton School, we’ve recently started learning the Python programming language. After a brief foray into functional programming in Python, there has been a paradigmatic shift toward object-oriented programming. To that end, we are going to explore the magical world of OOP, by way of ‘Gullah Gullah Island’ — if you’re not familiar, that’s wholly unfortunate and I feel for you, but fear not; I will explain.
What is Object Oriented Programming?
Object-oriented programming (OOP) is a programming paradigm based upon objects and classes (having both data and methods), which aims to incorporate two distinctly inherent advantages in its design. These are modularity and reusability. In Python, everything is an object and I mean everything. Data, in this instance (get it?) refers to the variables and attributes that describe objects. Methods, meanwhile, refer to the functions that act on said data and perform specifically defined tasks, usually tied to the overall purpose (end goal) of a program.
Modularity meaning —
Designing your program in such a way that your code is divided in a logical manner, into separate parts. Separate parts meaning different source files, which in Python are known as modules. The focus and goal of this compartmentalization is to have modules with little to no dependencies upon other modules. In other words: minimization of dependencies.
Reusability meaning —
Objects, which are usually instances of Classes(capitalized for importance), are used to interact with one another to design applications and computer programs. When you want to use a class in one of your programs, you make an object from that class, which is where the phrase “object-oriented” comes from.
id() is a built-in function which, as the name implies, refers to the identity of an object. It is a unique identifier, which is typically of either type: integer or long integer . This number is essentially the address of the object in memory.
It would look like this, for example:
>>> Knicks = 'suck'
>>> Nets = Knicks
type() is another built-in function which refers to the kind of data an object can hold, what it’s capabilities are and what functions/methods can be called on it. It also dictates how you access and/or manipulate the value in said variable. Python specifically, has (5) standard data types: numbers, strings, lists, tuples and dictionaries.
Here it is, in practice:
>>> Jan = ['Marcia', 'Marcia', 'Marcia']
Mutable objects —
Mutability is the concept of whether or not an object can be modified. Therefore, mutable means that these objects can be changed. In Python the following object types are mutable: lists (basically a sequence or array of values), dictionaries (key : value paired information), sets(unordered collection of unique elements) and byte arrays (sequence of bytes represented by integers between 0 and 256)
Immutable objects —
As you may have guessed from the above definition and the added prefix, immutable means that these objects cannot be modified. This applies to both built-in and created objects. The following object types are immutable: bytes, tuples(sequence of immutable objects), frozen sets (previously defined ‘set’ — but specifically containing immutable objects), strings and numbers(int, float).
Immutable objects are stored in memory as essentially
read-only. Numbers specifically, are an interesting case.The current implementation of Python 3 — keeps an array of integer objects for all integers between -5 and 256, therefore when you create an int in that range you actually just receive a reference to the existing object. The values from -5 to +257 are stored in said array when the Python Interpreter is loaded into memory, to speed up the accessing and retrieval of small numbers. These values are expanded from the built-in macros
Assignment vs Reference — (Aliases)—
In Python, all variable names are merely references to objects. This is a key distinction between Python and a number of other languages — C, for instance. In C, values are assigned to variables. This can be simulated in Python, however, via the process of — aliasing — which happens whenever one variable’s value is assigned to another variable, since variables are just names that store references to values. This is somewhat similar to the concept of pointers in C. Lastly, you would likely only want to do this with immutable objects in Python, since aliasing means that your two hypothetical variables refer to the same object in memory, and therefore any changes to one variable’s value will be reflected on the other.
Functions, argument passing and implications for objects —
In regards to functions — parameters are passed by object (assignment), via variables. This is due to the fact that said parameters and variables are actually only references to objects, as aforementioned. Essentially, when you call a function with a parameter, a new reference of an object is created, which refers to the object passed in.
Ok, so we didn’t get there this time, but in my next blog post — ‘Let’s all go to Gullah Gullah Island’ — and we will, I promise. No matter what the computing concept is…
This is just a brief overview and introduction into the world of Python’s Object Oriented Programming (OOP) — stay tuned for more…