How is Python Different from Other Programming Languages

Understanding the concept of everything being an object in Python.

Ethan Mayer Bloom
Python in Plain English

--

What makes Python different than C and other programming languages? Well, everything is an object! Understanding this requires a fundamental shift in thinking about core language concepts like variables and functions. In this post, we’ll see what happens behind the scenes when we do common things like creating a variable. We will also go over immutable vs mutable objects and how Python treats them differently.

Variable Identifiers

To understand what happens behind the scenes with variable creation, we would use the type and id built-in methods. Type(variable) returns the class type of the argument (object) passed as a parameter. What do we mean by class type? As said earlier, everything in Python is an object, which means every object is an instance of some class. id(variable) returns the identity of the argument (object) passed as a parameter. This identity must be unique and constant throughout the lifetime of the object. In C, we could think of this as the memory address of a variable, but here in Python, it is a unique id. Think of it as a social security number.

The id method is rarely used, except for cases when we want to check if two variables refer to the same object. If this is true, they would have the same id number, thus they would be the same.

The last line of The Zen Of Python is useful for this article —

“Namespaces are one honking great idea — let’s do more of those!”

If we were to write int a = 5 in C, we would say that a is a variable of type int and contains the value 5. In Python, the line a = 5 would mean that a is a name that refers to the object 5. Names are essentially a way to access an underlying object, as we can see in the following example:

a = 5
print(id(a))
print(id(5))
print(id is a)
print(id == a)
Output:
10105216
10105216
True
True

It’s important to understand the difference between == and is. While they both would return True in this example, they do not compare the same thing. == compares the values of two objects, and is compares the id number of two objects. In this case, a is a name that refers to the object 5 , therefore they both have the same id number and value.

Let's mess things up a little :)

a = 2
print(id(a))
a = a + 1
print(id(a))
print(id(3))
b = 2
print(id(b))
Output:
10105120
10105152
10105152
10105120

Here’s a diagram that clarifies what is happening here.

Once we changed the reference of the name a to the object 3, its id number changed accordingly. By assigning the object 2 to the name b, we got the same id as a had earlier.

It’s a bit confusing to think of every variable as a reference to some object, but this also explains why there is no need to specify the type of the name in Python. a could be linked to a list, a string, or a number, but all it is is a reference.

Another example that’s important to understand:

a = 89
b = a
print(b is a)
Output:
True

When we make the name b equal to the name a, all we’re doing is giving b the same reference that a refers to (object 89), thus we created two names with the same object. When checking a is b , we’re just checking if the object 89 is equal to itself.

Mutable and Immutable Objects

There are two types of objects in Python, mutable and immutable. The type of the object is assigned at runtime and cannot be changed, but its state can be modified if it is mutable.

Mutable objects:

  • lists
  • dictionaries
  • sets

Immutable objects:

  • int
  • floats
  • strings
  • tuples

For example, if we try to change a character in a string object:

str1 = "Hello World"
str1[2] = 'x'
print(str1)
Output:
TypeError: 'str' object does not support item assignment

The object is immutable hence the error. However, what if we did:

str1 = "Hello World"
print(id(str1))
str1 += 'x'
print(str1)
print(id(str1))
Output:139654666599984
Hello Worldx
139654626995632

Technically, we did modify the string. But if you notice the id number before and after the modification, it changed! Every object has its unique id as said earlier, which means that we did not modify the string object, we just created a new one.

If we were to define two names that refer to the same string, are they equal?

a = "banana"
b = "banana"

These are the possible options; Either a and b point to the same object, or they each have their own. We can check this using the is operator:

a = "banana"
b = "banana"
print(a == b)
print(a is b)
Output:
True
True

This means that banana is one object, with a and b being names that point to it. If we try this with other data types, different things happen:

a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)
Output:
True
False

Ah ha, the objects are not the same (have different IDs). Every list is its object, even if the value of it is the same. Why would this be different than strings? Well, what’s the difference between lists and strings? Strings are immutable, while lists are. If a and b were to point to one list object, any change made with a would affect b, and we could accidentally cause unwanted changes. If one would need this behavior, we could create an alias to the original name. a = [1, 2, 3] and b = a. Now b refers to the same object that a refers to, and any change with either of them would affect the other.

Let’s take a look at a neat example:

a = [1, 2, 3]
b = a
a.append(4)
print(b)
print(b is a)
Output:
[1, 2, 3, 4]
True

b links to the same object that a links to, so we see the changes through both names. What if we were to modify the object as such:

a = [1, 2, 3]
print(id(a))
b = a
a = a + [4]
print(id(a))
print(b)
print(b is a)
Output:
139654626923464
139654626929608
[1, 2, 3]
False

Just like before with the example of the string, once we modified our list object, its unique id changed meaning the object itself is different. The name b points to the first object that a points to. Once a is updated with the new object, b still points to the previous one. If we were to assign b = a after the modification, b would print the new object.

Why Does It Matter?

Understanding the difference between mutable and immutable objects will help you know when to use each one. Mutable objects can be accessed faster than immutable objects, and are preferred when variables will stay the same throughout their lifetime. If we know that the value of an object is going to be modified, we would use mutable objects.

Function Arguments

In Python, arguments to functions are passed by assignment. The logic behind this is that the parameter is a reference to an object. In C, we know of passing-by-value and passing-by-reference. This is how this concept works in Python —

  • If you pass a mutable object into a method, that method receives a reference to the same object and you can mutate it as you’d like. If you re-assign this reference, the outer scope will know nothing about the change, therefore it will still point to the original object.
  • If you pass an immutable object into a method, that method receives a reference to the same object just like before, but here you cannot mutate or re-assign this reference.

Let’s go over some examples for clarification:

def modify_list(list):
list.append(4)
def change_list(list):
list = list + [4]
list = [1, 2, 3]
modify_list(list)
print(list)
list = [1, 2, 3]
change_list(list)
print(list)
Output:
[1, 2, 3, 4]
[1, 2, 3]

As shown, the first function modify_list modifies the object itself and not the reference, therefore the outer-scope object changes as well. In the second function change_list, we are assigning a new value to the reference, so the original object is not touched. In this example, we use a mutable object. Let’s go over an immutable object:

def assign_value(a, b):
a = b
str1 = "Hello"
assign_value(str1, "World")
print(str1)
Output:
Hello

We are changing the value of the reference in the function, so nothing changed with the real object.

Before passing an argument to a function, think of what type of object you are passing and whether you would like to modify this object inside the function.

Conclusion

To sum up, Python’s core concepts are different from others, and it’s important to understand them to write more efficient code.

Thank you for reading.

More content at plainenglish.io. Sign up for our free weekly newsletter here.

--

--