Introducing Object-oriented programming
or: how a fledgling programmer learned to stop worrying and love Python3
What is an object?
The dictionary definition for object is:
- a material thing that can be seen and touched.
“he was dragging a large object”
synonyms: thing, article, item, device, gadget, entity
- a person or thing to which a specified action or feeling is directed.
“disease became the object of investigation”
synonyms: target, butt, focus, recipient, victim
“he spent five years as the object of a frenzied manhunt”
In programming, this definition of object can be extended — though you may not be see and touch software objects, you can definitely perform actions on objects. According to Dusty Phillips in Python 3 Object-oriented Programming, “an object is a collection of data and associated behaviors.”
So what does it mean when we add “oriented” after object? Imagine a city map and how it can be used to direct us to landmarks and destinations — now think of GPS and the blue “my location” dot. That dot is what orients us in relation to our surroundings so that we can direct ourselves to the correct destination. In software development, to be “object oriented” is to be directed towards modeling objects.
What is the difference between a class and an object or instance
In addition to objects, the other main aspect of object oriented programming are classes. Again, Philips describes the difference between objects and classes beautifully:
Classes describe objects. They are like blueprints for creating an object. You might have three oranges sitting on the table in front of you. Each orange is a distinct object, but all three have the attributes and behaviors associated with one class: the general class of oranges.
A class creates a new type where objects are instances of the class. Another way to state this is that an object instance is a specific object with its own set of data and behaviors.
Mutable or immutable?
In Marvel’s X-Men series, the X-Men are mutants, a subspecies of humans who are born with superhuman abilities. In Python, there are some data types that have special abilities to be changed and, like X-Men, this is referred to as mutability. This is an important aspect to Python gives software developers the ability to change data quickly and efficiently.
In Python, objects that can be changed are called mutable. Likewise, objects that can not be changed are referred to as immutable.
When I searched Google for an illustration of mutable types in Python, I kept seeing this table:
To unpack this table, generally data types in Python can be distinguished based on whether objects of the type are mutable or immutable. The content of objects of immutable types cannot be changed after they are created.
Some immutable types:
- int, float, long, complex
- frozen set
Some mutable types:
- byte array
Only mutable objects support methods that change the object in place, such as reassignment of a sequence slice, which will work for lists, but raise an error for tuples and strings — continue reading to see a demonstration of this!
Python has several built-in functions that allow you to test objects.
id() allows you to test if a type is mutable or not. You can also use it to test if items are identical or not, since
id() returns the exact memory address of an object.
type() allows you quickly find out the type of an object
Since variables refer to objects, if we assign one variable to another, both variables refer to the same object. Since the same list
[1, 2, 3] has two different names, we say that it is aliased. As the gif below demonstrates, changes made to one alias affect the other.