Mutable, Immutable… everything is object!

Life is full of assignments. Some assignments can be changed while others are stuck the way they are, either way assignments will always be a part of everyones life. The same goes with the concepts of software engineering and how assignments play a major role in a programmers task. In this article I am going to discuss assignments within computer programming. Within the context of this article I am going to dig deeper and talk about everything that has to do with assignments and hopefully by the end you will have a clear idea of what they are.

In a lot of computer programming languages there is a term called variables that assigns a number, string, list and the like to a object. In many other languages when you assign a variable to a object it virtually puts it into a box, or in software engineering terms, the variable is placed in a memory address.

a = “max”

Box a now contain the variable string “max”. Now lets say we want to reassign a different variable to the object a.

a = “Holberton”

Now the object a has the variable Holberton instead of Max. Now lets create a new object named b and assign a to b.

b = a

What do you think b is? If you said “Holberton” you are correct. This is called value reassigning and this will play a major role as we move a long, but for now let’s move on to something else.

Variable is an object..but I thought a value is a variable…but everything is an object?!?!?

What is a object

In order to understand objects we have to understand the concept of classes. Classes could be looked at the same way you viewed them as a adolescent, no not a pain in your rear end but something with a subject, a teacher and students. To start let’s say that the class name is “History” and the subject or in our case the object is food (yes the history of food. In coding language that would look like

class History(food)

Now lets define food, we can start with the topic of a hot dog. Hot dogs come with a variety of thing such as relish, ketchup, mustard, onions, hot dog bun etc. So we will first define hot dog with bun under the class of History and the object of food.

class History(food):
def bun(self)
def ketchup(self):
def mustard(self):

All of these are a part of the History class and everything within it is a object. To understanding things a little bit better just think of a class as another way of representing some other sort of data/information.

Now let me ask you what is History? It’s a type of class. Next let me ask you what is food? It is a type of object. Everything wihtin coding is a type. As I mentioned before there are variables and objects. Let’s say you and your friends are going out on the town and head to the bar, what is the first hting they ask you when you enter the bar? They ask for your id. Every time a variable, a list, a tuple, a string is presented it comes with a id.

a = “Hello”
id(a)
436764987082

The id() function is a built in function of python 3, and as you can tell it returns the identity of an object. The identity if a unique combination of numbers(integers) for that object during the cycle of the running program until it is closed. You could also think of id number as the memory address. Remember that the argument for an object can be a int, float, str, tuple, list, and so on.

“But why are some addresses different from others”?

In python some objects or mutable(changeable) while others are unmutable(unchangeable). However (before I contradict myself), every time you define a object a new memory address is set. But let’s think back to the earlier part of this essay where we set a to a value and then b to a. B had the same output as a, so when we look for the id for id(a) and id(b) we get the same memory output. But there are some exceptions , for instance when the numbers are between -5 and 256 and small strings that are less than 20 characters will have the same id.

When it comes to immutable objects such as tuples the memory address with always be different no matter what. For instance if you set two different tuples with the same value there id’s will still be different and this is the same for all immutable objects, for instance:

>>> a = (1,)
>>> b = (1,)
>>> id(a)
140416206836792
>>> id(b)
140416206836904

IS vs ==

To check the value or the object of a variable one must use is or ==. To keep it simple we will you the aforementioned a and b objects. Let look at the example below:

a is b
True
a == b
True
a = 2
b = 3
a is b
False
a == b
False

Call this and Call that…No one now a days calls anyone back

If you are new to coding I am sure that you always here the term call. It confuses a lot of people and I understand the confusion so I will try to break it down.

Call by Value

aka pass by value, when you hear the word call t first looks at the argument expression(function name) and is evaluated. The outcome of the evaluation is what is within the function variable and if if it i a variable a local copy of its value will be used.

Call by Reference

aka pass by reference, a function gets a reference to the argument instead of a copy of its value. The side effect of this is that the function can modify the argument(value of called variable) scope can be changed. Programmers do this to save time which really isn't detected by the human eye but helps when it comes to efficient programming. So if we have a list we have to consider that th elements of a list can be changed within the callers scope. However the old list will not be affected. Example below

x = 9
id(x)
579895803
ref_function(x)
x = 9 id =579895803
x = 42 id = 55495408
id(x)
79895803

Python starts out with call by reference but as soon as you change the value it switches to, you got it call by value.

Well thats it. I hope this we very informative to you and you enjoyed the read. Software engineering is tough but very enjoyable once you are able to wrap our head around things. And for anyone who is interested in software engineering I suggest you check out Holberton School, It is one of the best schools around that will teach you the in and outs of Software engineering.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.