Things I learned about objects in Python 3

Jimmy Thong
meatandmachines
Published in
4 min readJan 27, 2017
Source

Jeff Goodell: Would you explain, in simple terms, exactly what object-oriented software is?

Steve Jobs: Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here.

Here’s an example: If I’m your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes laundered, please.” I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, “Here are your clean clothes.”

You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can’t even hail a taxi. You can’t pay for one, you don’t have dollars in your pocket. Yet I knew how to do all of that. And you didn’t have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That’s what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.

[source]

When you tell Python to make a new kind of thing, you create a class. An object is the most basic kind of thing (within that class). To quote the famous: “everything in Python is an object. Strings are objects. Lists are objects. Functions are objects. Even modules are objects.”

In real life terms, let’s take a person for example. A class is a blueprint to make a person. People built from that blueprint are objects. A respective [specific representation of a] person is an instance.

Every object has an id(). An id can be thought of as a memory in address. Each individual object has a respective id, represented by an integer [which is the object’s address in memory]. You can compare if two variables reference the same object in Python based on their ids, if they are the same integer, they reference the same object.

Every object has a [data] type(). There are five standard data You can find out about the many kind of [data] types in Python 3: numbers, strings, lists, tuples and dictionaries.

Whether we can change the contents of objects or not in Python, we have to talk about Mutation in Python. Simply: if an object is immutable, its contents cannot be changed. If an object is mutable, its contents can be changed.

The following are immutable objects:

Numeric types: int, float, complex

string

tuple [note: immutable container]

frozen set [note: immutable version of set]

bytes

The following objects are mutable [note: all containers]:

list

dict

set

byte array

[source]

Mutability matters because one could risk inefficiency by wasting a lot of memory in throwing away objects. And I won’t be able to say it better than this guy, so take it away Tommy!

Source

How arguments are passed to functions and what this implies for mutable and immutable objects has to do with evaluation strategies [for arguments]. The most common evaluation strategies are “call by value” and “call by reference.” The difference is “call by reference” saves computation time and memory space because the function gets an implicit reference to the argument, rather than a copy of its value (when you “call by value”). As aforementioned, mutable objects in Python are containers/collections and need to be copied, so they need to be “call[ed] by value.” Immutable objects are “call[ed] by reference” because they do not need to be copied.

--

--

Jimmy Thong
meatandmachines

Makerspace Teacher, Code Coach, Amateur Home Cook, Writer