“First-class everything”

Coded in Python 3.4

“First-class everything” a principle which I found to be astonishing. The creator of Python Guido Van Rossum designed the language in accordance with that principle. What it essentially means that all the functions, classes, modules, methods and data types all have equal status.

“Everything is treated the same way.”

Based of that princes the terminology objects was born.
An object is something a variable can refer to.
Python does an excellent optimizing memory allocation thus when it comes to some data type that holds the same value but is under a different name then it will be given a reference to the same value rather than its own copy. To better understand this take a look at the follow example:

Python3 Interpreter:

>>> a = "banana"
>>> b = "banana"
>>> a == b
True
>>> a is b
True

This is how it looks under the hood:

In order to differentiate whether an assignment is a reference or not Python has implemented two built in id() and type() that make it easy.


Id and Type:

The id(object)built-in takes in an object and output the memory address / identity of that object.

>>> a = "banana"
>>> b = "banana"
>>> id(a)
1238372698
>>> id(b)
1238372698
>>> id(a) == id(b)
True

The type(object) built-in takes in one argument and returns the type the object is. To better understand what this means I will explain what common types of objects there are in Python.

List is an ordered set of values enclosed in square brackets, separated by comma where each value can be accessed via index. These values are called elements.
There can be various objects within a list.

>>> list_a = [‘a’, 1, [2, 3], (4, 5), {‘b’: 6}] # setting list
>>> len(list_a) #getting length of list
5
>>> list_a[2][0] #getting value from list
2

Tuples, are like list but can not be modified (will talk about it later on). They are written much like list but by using parentheses instead of square brackets.
Elements within tuples can not be assigned to.

>>> tuple_a = (1, 2, 3) # setting tuple
>>> len(tuple_a) # getting length of tuple
3
>>> tuple_a += (4, ) #appending to tuple
>>> tuple_a
(1, 2, 3, 4)

Dictionary (dict) consist of unordered set of elements. Where each element must have a unique string or object as a key and any string or object as value. All the elements are surrounded by curly brackets and separated by a comma.

>>> dict_a = {‘anoop’: 23, ‘Minas’: 27, ‘Sid’: 4}
>>> dict_a[‘anoop’] = 1
>>> dict_a
{‘anoop’: 1, ‘Minas’: 27, ‘Sid’: 4}

Strings and numbers nothing more than primitive data types.

>>> int_a = 1
>>> str_a = ‘b’

Mutable Objects:

Now we know how that some objects can either hold a copy of the content or merely reference it. However, the objects that do hold reference can later take on a copy when the data is being manipulated. 
This is called a mutable object. A mutable object is an object whose contents can be modified.
Lists, dict are mutable objects.

Immutable objects:

Immutable objects on the other can not be modified.
Numbers, strings, and tuples are immutable objects. Tuples do not have any methods that modify them as lists do and similarly numbers and strings.

why does it matter and how differently does Python treat mutable and immutable objects?

It’s important to know whether an argument is mutable or immutable so you can understand the expected behavior when passing it around.
For example, when passing a list as an argument, it’s important to note that any changes made to the list inside the function will be reflected out side of it as well because you’re actually passing the reference to the list, not a copy of it. Similarly with dict. Knowing whether or not it is mutable will help you determine if you need a return statement or prevent unwanted modification.

>>> def capitalize(list)
... for i, letter in enumerate(list):
... list[i] = letter.capitalize()
... return list
>>> list_before = [a, b, c]
>>> list_after = capitalize(list_before)
>>> print(list_before)
[A, B, C]
>>> print(list_after)
[A, B, C]
>>> list_before is list_after
True

“This story was created for educational proposes so that I can better understand the concepts of Objects in Python by articulating it to a third person. I apologize for any mistakes made and please do not hesitate to correct me -for this is a learning experience.”