Python: Objects
Introduction
“Everything is object”. I’m going to explain some features in python that are so important to know and understand, in order to avoid getting unexpected behaviors of your python programs in the future. Guido van Rossum has designed the language according to the principle "first-class everything", meaning that he made everything in a way that they can be manipulated (or treated) in the same way. to further explain, Guido said "I meant that I wanted all objects that could be named in the language to have equal status". (Blog, The History of Python, February 27, 2009). This could be confusion, but yes, functions and methods are values just like integers, strings and lists.
id and type
You might be asking yourself "Why should I know these functions ? and why are they related to this blog?", well, you need to know them because I'm going to use them to explain stuff. Okay, let's start with id(); it's a function that returns a number, it's the identifier of the variable, If you are familiar with C, id returns the memory address of the variable. Now let's take a look at this example
>>> ch1 = "string" >>> ch2 = "string" >>> id(ch1) 139632 >>> id(ch2) 139632
Omg look! id(ch1) is equal to id(ch2) ... well that's confusing. we'll see more about this later.
Now let's have a little look at type() method, it returns the type of the variable we entered. let's look at the example below:
>>> def func(): ... return 2 ... >>> type(func) <class 'function'> >>> number = 2 >>> type(number) <class 'int'>
as we can see, this confirms what was said earlier "first-class everything"
mutable objects
Python, has two types of objects, those that are mutable and those that are immutable. We'll take a look at mutable objects first. So, what are mutable objects and which ones are they;
mutable objects are those that we can change their values, like lists, and dictionaries, so we can assign new values in them, we can change they contents... simply they are mutable
>>> l = [1, 2, 3] >>> l += [29] >>> print(l) [1, 2, 3, 29] >>> l[2] = "string" >>> print (l) [1, 2, 'string', 29]
as you see here, you can simply add objects ...
anyway, the real question here is: how to spot a mutable type !?
well let's walk through these examples below:
>>> l = [1, 2, 3, 4] >>> def change(l=[]): ... l[1] = 20 ... >>> change(l) >>> print (l) [1, 20, 3, 4] #it wouldn't change if it was immutable
another example:
>>> l1 = [1, 2, 3, 4] >>> l2 = l1 >>> l2[2] = "lol" >>> print (l2) [1, 2, 'lol', 4] >>> print (l1) #l1's value changed when we changed l2 [1, 2, 'lol', 4] # wouldn't happen in an immutable object
If u find this annoying and u want to just take a copy from l1 to l2 then you can just clone it like this
l2 = l1[:]
like this, you can simply change l2 and l1 will be intouched.
immutable objects
it can be confusing for immutable objects, saying that they can't be changed and seeing this example
>>> num = 2 >>> num 2 >>> num = 5 >>> num 5
the thing is that the number didn't change, it's the reference of num changed, which means that the num identifier changed.
>>> num = 2 >>> id(num) 10914528 >>> num = 5 >>>> id(num) 10914624
anyway, let's spot see what makes strings, integers and tuples immutable
>>> def change (x): ... x += 1 ... >>> x = 2 >>> x 2 >>> change (x) >>> x 2 # see !!! didn't change
another example:
>>> x = 2 >>> y = x >>> y 2 >>> y = 29 >>> x 2 # didn't change, unlike mutable objects.