|
| 1 | +# Everything in Python is an Object |
| 2 | + |
| 3 | +**Video link:** [https://youtu.be/X1RN6ADsOW4](https://youtu.be/X1RN6ADsOW4) |
| 4 | + |
| 5 | +In this video, you built a solid knowledge of Python objects by learning to check the type of objects, listing their attributes & methods, and understanding what actually goes under the hood. |
| 6 | + |
| 7 | +**Programs in the Video** |
| 8 | + |
| 9 | +- [The type() function](#the-type-function) |
| 10 | +- [The dir() function](#the-dir-function) |
| 11 | +- [The id() function](#the-id-function) |
| 12 | +- [How variables actually work](#how-variables-actually-work) |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## The type() function |
| 17 | +Everything in Python is already an object, whether it's strings, numbers, functions or even classes. |
| 18 | + |
| 19 | +We can check this using the type() function. |
| 20 | + |
| 21 | +```python |
| 22 | +numbers = [1, 4, 9, 16] |
| 23 | +print(type(numbers)) |
| 24 | +``` |
| 25 | + |
| 26 | +**Output** |
| 27 | + |
| 28 | +``` |
| 29 | +<class 'list'> |
| 30 | +``` |
| 31 | + |
| 32 | +This means that lists are instantiated from the built-in Python class named `list`. |
| 33 | + |
| 34 | +Let's try other objects: |
| 35 | + |
| 36 | +```python |
| 37 | +numbers = [1, 4, 9, 16] |
| 38 | +print(type(numbers)) |
| 39 | + |
| 40 | +n1 = 5 |
| 41 | +print(type(n1)) |
| 42 | + |
| 43 | +flag = True |
| 44 | +print(type(True)) |
| 45 | + |
| 46 | +def my_function(): |
| 47 | + pass |
| 48 | + |
| 49 | +print(type(my_function) |
| 50 | +``` |
| 51 | + |
| 52 | +**Output** |
| 53 | +``` |
| 54 | +<class 'list'> |
| 55 | +<class 'int'> |
| 56 | +<class 'bool'> |
| 57 | +<class 'function'> |
| 58 | +``` |
| 59 | + |
| 60 | +We can see that all these entities are instantiated from a class, which means they are all objects. |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## The dir() function |
| 65 | +We can list out all the attributes and methods of a given object by using the `dir()` function. |
| 66 | + |
| 67 | +```python |
| 68 | +numbers_list = [1, 2] |
| 69 | +print(dir(numbers_list)) |
| 70 | +``` |
| 71 | + |
| 72 | +**Output** |
| 73 | + |
| 74 | +``` |
| 75 | +['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] |
| 76 | +``` |
| 77 | + |
| 78 | +Let's use the `__add__` method. It is used to append all the items of another list to the end of the list. |
| 79 | + |
| 80 | +```python |
| 81 | +numbers_list = [1, 2] |
| 82 | + |
| 83 | +print(dir(numbers_list)) |
| 84 | + |
| 85 | +result = numbers_list.__add__([3, 4]) |
| 86 | +print(result) |
| 87 | +``` |
| 88 | + |
| 89 | +**Output** |
| 90 | +``` |
| 91 | +[1, 2, 3, 4] |
| 92 | +``` |
| 93 | + |
| 94 | +We can also accomplish this task by using the plus operator. |
| 95 | + |
| 96 | +```python |
| 97 | +numbers_list = [1, 2] |
| 98 | + |
| 99 | +print(dir(numbers_list)) |
| 100 | + |
| 101 | +# result = numbers_list.__add__([3, 4]) |
| 102 | +result = numbers_list + [3, 4] |
| 103 | + |
| 104 | +print(result) |
| 105 | +``` |
| 106 | + |
| 107 | +**Output** |
| 108 | + |
| 109 | +``` |
| 110 | +[1, 2, 3, 4] |
| 111 | +``` |
| 112 | + |
| 113 | +In fact, the plus operator internally calls the `__add__()` method. Even though we are working with operators, we are actually using attributes and methods of the object internally. |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## The id() function |
| 118 | + |
| 119 | +In Python, every object has an id for identity. The id of an object is always unique and constant for this object during its lifetime. |
| 120 | + |
| 121 | +We can check the id of an object by using the `id()` function. |
| 122 | + |
| 123 | +```python |
| 124 | +number1 = 5 |
| 125 | +print(id(number1)) |
| 126 | + |
| 127 | +number2 = 10 |
| 128 | +print(id(number2)) |
| 129 | +``` |
| 130 | + |
| 131 | +**Output** |
| 132 | + |
| 133 | +``` |
| 134 | +9784992 |
| 135 | +9785152 |
| 136 | +``` |
| 137 | + |
| 138 | +Let's try assigning `number1` to `number2`. |
| 139 | + |
| 140 | +```python |
| 141 | +number1 = 5 |
| 142 | +print(id(number1)) |
| 143 | + |
| 144 | +number2 = number1 |
| 145 | +print(id(number2)) |
| 146 | +``` |
| 147 | + |
| 148 | +**Output** |
| 149 | + |
| 150 | +``` |
| 151 | +9784992 |
| 152 | +9784992 |
| 153 | +``` |
| 154 | + |
| 155 | +The id is the same. |
| 156 | +Python does this for memory optimization. |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +## How variables actually work? |
| 161 | + |
| 162 | +We have been learning that variables store a value. However, this is not technically true. |
| 163 | + |
| 164 | +A variable is more like a name tag and it can refer to any value. |
| 165 | + |
| 166 | +Suppose, we have a list `a` and we assigned another variable `b` to this variable: |
| 167 | + |
| 168 | +```python |
| 169 | +a = [1, 2, 3] |
| 170 | +b = a |
| 171 | +``` |
| 172 | + |
| 173 | +Now, if we try to change the value of `a`: |
| 174 | + |
| 175 | +```python |
| 176 | +a = [1, 2, 3] |
| 177 | +b = a |
| 178 | + |
| 179 | +a.append(4) |
| 180 | + |
| 181 | +print("a =", a) |
| 182 | +print("b =", b) |
| 183 | +``` |
| 184 | + |
| 185 | +**Output** |
| 186 | +``` |
| 187 | +[1, 2, 3, 4] |
| 188 | +[1, 2, 3, 4] |
| 189 | +``` |
| 190 | + |
| 191 | +Here, both variables `a` and `b` are changed. It's because `a` and `b` are referring to the same object. And, if we check the id of variables `a` and `b`, they will be the same. |
| 192 | + |
| 193 | +That's why we use the list's `copy()` method to copy one list to another if we do not want this behavior. |
| 194 | + |
| 195 | +```python |
| 196 | +a = [1, 2, 3] |
| 197 | +b = a.copy() |
| 198 | + |
| 199 | +a.append(4) |
| 200 | + |
| 201 | +print("a =", a) |
| 202 | +print("b =", b) |
| 203 | +``` |
| 204 | + |
| 205 | +**Output** |
| 206 | +``` |
| 207 | +[1, 2, 3] |
| 208 | +[1, 2, 3, 4] |
| 209 | +``` |
| 210 | + |
| 211 | +Now, `a` and `b` refer to two different objects and modifying one won't affect the other. |
0 commit comments