Hello, Nice to meet you, I’m an object.

INTRODUCTION

ID AND TYPE, WHAT IS THAT?

>>> i = "Hello"
>>> id(i)
***704
>>> j = "World"
>>> id(j)
***736 (different from ***704)
>>> i = "Hello"
>>> j = "World"
>>> id(i)
***139725014575280
>>> id(j)
***139725014575280 (same id)
>>> obj1 = "Julien"
>>> type(obj1)
<class 'str'>

MUTABLE OBJECTS

>>> str1 = "Julien might win at Starcraft"
>>> str2 = " or he might lose."
>>> str1 += str2
>>> print(str1)
Julien might win at Starcraft or he might lose.
# Assigning two objects to the same string
>>> a = "b"
>>> b = "b"
>>> id(a), id(b)
(139873424855424, 139873424855424)# Both strings have the same id# Adding third object 'c'>>> c = "c"
>>> id(c)
139873424786912
>>> a += c
>>> print(a)
bc
>>> id(a)
139873423703824

INMUTABLE OBJECTS

>>> x = 24601
>>> x
24601

>>> x = 24602
>>> x
24602
>>> x = 24601
>>> x
24601

>>> id(x)
1470416816

>>> x = 24602
>>> x
24602

>>> id(x)
1470416832

HOW ARGUMENTS ARE PASSED TO FUNCTIONS AND WHAT DOES THAT IMPLY FOR MUTABLE AND INMUTABLE OBJECTS

def updateList(list1):
list1 += [10]n = [5, 6]
print(id(n)) # 140312184155336updateList(n)
print(n) # [5, 6, 10]
print(id(n)) # 140312184155336
def updateNumber(n):
print(id(n))
n += 10b = 5
print(id(b)) # 10055680
updateNumber(b) # 10055680
print(b) # 5

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store