-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP-CLASS&OBJECT.py
More file actions
611 lines (420 loc) · 20.4 KB
/
OOP-CLASS&OBJECT.py
File metadata and controls
611 lines (420 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
########## OOP - CLASS and OBJECT ##########
#### CLASS
# A CLASS is an idea which can be used to create a number of incarnations
# such an incarnation is called an OBJECT
# When a class is derived from another class, their relation is named inheritance
## define a class
class TheSimplestClass:
pass
## inheritance
class Alpha: # superclass of Beta
value = "Alpha"
def say(self):
return self.value.lower()
class Beta(Alpha): # subclass of Alpha()
pass
#### OBJECT/INSTANCE OF A CLASS
# An object is one particular physical instantiation of a class that occupies memory and has data elements
# This is what 'self' refers to when we deal with class instances
# !! An object is everything in Python that you can operate on, like a class, instance, list, or dictionary
# The term instance is very often used interchangeably with the term object, because object refers to a particular instance of a class
# It’s a bit of a simplification, because the term object is more general than instance !!
# An instance is an incarnation of the requirements, traits, and qualities assigned to a specific class
# Every existing object may be equipped with three groups of attributes:
# a name that uniquely identifies it within its home namespace
# a set of individual properties which make it original, unique, or outstanding
# a set of abilities to perform specific activities
# Whenever we describe an object we use:
# a noun = object's name
# an adjective = object's property
# a verb = object's activity
# example:
# A pink Cadillac went quickly.
# ==>
# Object name = Cadillac
# Home class = Wheeled vehicles
# Property = Color (pink)
# Activity = Go (quickly)
## define an object
my_first_object = TheSimplestClass()
# The act of creating an object of the selected class is also called an instantiation
# The object becomes an instance of the class
#### CLASS & OBJECT VARIABLES
# ==> Exemple
class Animal:
espèce = "Mammifère" # Variable de classe
def __init__(self, nom):
self.nom = nom # Variable d'instance
# Toutes les instances partagent la variable de class "espèce"
a1 = Animal("Lion")
a2 = Animal("Tigre")
print(a1.espèce)
# Mammifère
print(a2.espèce)
# Mammifère
## Modifier un variable
# une variable de class peut etre modifiée via la Class ==> GOOD PRACTICE
Animal.espèce = "Cétacé"
print(a1.espèce)
# Cétacé
print(a2.espèce)
# Cétacé
# une variable de class peut etre modifiée via l'Objet ==> BAD PRACTICE
# ==> cela crée un doublon de nom de variable, cela fonctionne mais ça n'est pas cohérent
a1.espèce = "Poisson"
print(a1.espèce)
# Poisson # la variable d'instance prend le pas sur la variable du meme nom de la Class
print(a2.espèce)
# Cétacé
# une variable peut etre ajoutée à une instance de façon indépendante
a2.color = 'red'
print(a2.color)
# red
## Cas des variables privées
class BluePrint:
__element = 1
def __init__(self):
self.component = 1
def __action(self):
pass
product = BluePrint()
print(product.__dict__)
# {'component': 1} # 'component' via __init__
# Modifier via l'instance ==> BAD PRACTICE
product._BluePrint__element +=1 # va chercher la variable de Class et en crée une spécifique à l'Objet, indépendante de celle de de Class, et l'augmente de 1
print(BluePrint._BluePrint__element) # n'agira pas sur la variable de Class
# 1
print(product._BluePrint__element) # agira bien sur la variable d'objet
# 2
print(product.__dict__)
# {'component': 1, '_BluePrint__element': 2} # la variable est bien dans le object.__dict__ maintenant,
# MAIS le nom est trompeur, il est inspiré de celle de Class et personnalisé en valeur...
# Modifier via la Class
BluePrint._BluePrint__element +=1
print(BluePrint._BluePrint__element) # +1 sur la variable de Class
# 2
print(product._BluePrint__element) # aucun changement sur la variable d'Objet
# 2
## class variable remains the same for all instances using the class
# if the class variable changes, it changes for ALL instances variables
class A:
X = 0
def __init__(self,v = 0):
self.Y = v
A.X += v
a = A() # A.X += v ==> X = 0
b = A(1) # A.X += v ==> X = 0 + 1 = 1
c = A(2) # A.X += v ==> X = 1 + 2 = 3
print(c.X)
# 3
## INSTANCE.__DICT__
print(a1.__dict__)
# {'nom': 'Lion', 'espèce': 'Poisson'} # 'nom' via __init_ + 'espèce' via l'objet en direct
print(a1.Animal.espèce)
# AttributeError: 'Animal' object has no attribute 'Animal'
print(a2.__dict__)
{'nom': 'Tigre', 'color': 'red'} # 'nom' via __init_ car + 'color' via l'objet en direct ET 'espèce' n'apparait pas car c'est une variable de Class
print(a2.espèce) # mais si on invoque la variable on l'a bien
# Cétacé
## DIR(INSTANCE)
print(dir(a1))
# ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__',
# '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__',
# '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
# '__setattr__', '__sizeof__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__', 'espèce', 'nom'] ==> 'nom' via Class.__init__
# + 'espèce' en direct
print(dir(a2))
# ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__',
# '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__',
# '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
# '__setattr__', '__sizeof__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__', 'color', 'espèce', 'nom'] ==> 'nom' via Class.__init__
# + 'espèce' via la class
# + 'color' en direct
## INSTANCE.__DIR__
print(a2.__dir__)
# ['nom', 'color', '__module__', '__firstlineno__', 'espèce', '__init__', '__static_attributes__', '__dict__',
# '__weakref__', '__doc__', '__new__', '__repr__', '__hash__', '__str__', '__getattribute__', '__setattr__',
# '__delattr__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__reduce_ex__', '__reduce__',
# '__getstate__', '__subclasshook__', '__init_subclass__', '__format__', '__sizeof__', '__dir__', '__class__']
## CLASS.__DICT__
class Animal:
espèce = "Mammifère" # Variable de classe
def __init__(self, nom):
self.nom = nom # Variable d'instance
# Toutes les instances partagent la variable espèce
a1 = Animal("Lion")
a2 = Animal("Tigre")
print(Animal.__dict__)
# {'__module__': '__main__', '__firstlineno__': 1, 'espèce': 'Mammifère', ==> variable de Class 'espèce' + function __init__ (et toute autre fonction)
# '__init__': <function Animal.__init__ at 0x0000012AFE6A3E20>,
# '__static_attributes__': ('nom',), '__dict__': <attribute '__dict__' of 'Animal' objects>,
# '__weakref__': <attribute '__weakref__' of 'Animal' objects>, '__doc__': None}
## DIR(CLASS)
print(dir(Animal))
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__', '__ge__',
'__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__',
'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__static_attributes__', '__str__', '__subclasshook__', '__weakref__', 'espèce'] # ==> 'espèce' variable de Class
## CLASS.__DIR__
print(Animal.__dir__)
# <method '__dir__' of 'object' objects>
## .__DICT__ VS DIR()
class Cat:
Species = 1
def get_species(self):
return 'kitty'
class Tiger(Cat):
def get_species(self):
return 'tiggy'
def set_species(self):
pass
# ==> object
creature = Tiger()
print(creature.__dict__)
# {}
print(dir(creature))
# ['Species', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__',
# '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
# '__repr__', '__setattr__', '__sizeof__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__', 'get_species', 'set_species']
# ==> Class
print(Tiger.__dict__)
# {'__module__': '__main__', '__firstlineno__': 8, 'get_species': <function Tiger.get_species at 0x000001F4A1481940>,
# 'set_species': <function Tiger.set_species at 0x000001F4A1480C20>, # '__static_attributes__': (), '__doc__': None}
print(dir(Tiger))
# ['Species', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__',
# '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
# '__repr__', '__setattr__', '__sizeof__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__', 'get_species', 'set_species']
# ==> Superclass
print(Cat.__dict__)
# {'__module__': '__main__', '__firstlineno__': 1, 'Species': 1, 'get_species': <function Cat.get_species at 0x000001F4A1473E20>,
# '__static_attributes__': (), '__dict__': <attribute '__dict__' of 'Cat' objects>, '__weakref__': <attribute '__weakref__' of 'Cat' objects>, '__doc__': None}
print(dir(Cat))
# ['Species', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__',
# '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
# '__repr__', '__setattr__', '__sizeof__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__', 'get_species']
## __DICT__() in depth
class Classy:
varia = 1
def __init__(self):
self.var = 2
def method(self):
pass
def __hidden(self):
pass
obj = Classy()
print(obj.__dict__)
# {'var': 2}
print(Classy.__dict__)
# {'__module__': '__main__', '__firstlineno__': 1, 'varia': 1, '__init__': <function Classy.__init__ at 0x000001B773103E20>,
# 'method': <function Classy.method at 0x000001B773111940>, '_Classy__hidden': <function Classy.__hidden at 0x000001B773110C20>,
# '__static_attrib<function Classy.method at 0x000001B773111940>, '_Classy__hidden': <function Classy.__hidden at 0x000001B773110C20>,
# '__static_attributes__': ('var',), '__dict__': <attribute '__dict__' of 'Classy' objects>, '__weakref__': <attribute '__weakref__' of 'Classy' objectutes__': ('var',),
# '__dict__': <attribute '__dict__' of 'Classy' objects>, '__weakref__': <attribute '__weakref__' of 'Classy' objects>, '__doc__': None}
# {'__module__': '__main__',
# Module où la classe est définie
# '__firstlineno__': 1,
# ligne à laqualle la class commence
# 'varia': 1,
# attribut de class partagé par tous les objects
# '__init__': <function Classy.__init__ at 0x000001B773103E20>,
# constructor, et emplacement mémoire
# 'method': <function Classy.method at 0x000001B773111940>,
# method d'instance et emplacement mémoire
# '_Classy__hidden': <function Classy.__hidden at 0x000001B773110C20>,
# methode privée d'instance
# '__static_attributes__': ('var',),
# Tuple listant les attributs statiques
# '__dict__': <attribute '__dict__' of 'Classy' objects>,
#
# '__weakref__': <attribute '__weakref__' of 'Classy' objects>,
# Cet attribut permet la gestion des références faibles, référence à un objet qui ne
# l’empêche pas d’être collecté par le ramasse-miettes ==> ???
# Cela est utile pour éviter les cycles de références ou pour suivre des objets sans empêcher
# leur suppression.
# '__doc__': None}
# C’est la chaîne de documentation (docstring) de la classe ou de l’objet. Ici, elle est `None`,
# ce qui indique qu’il n’y a pas de documentation associée à cette classe ou cet objet.
## __NAME__
# contains the name of the class. It's nothing exciting, just a string
# it exists ONLY inside CLASSES
# so to find the class name of a particular OBJECT, you can use a function named type(),
# which is able (among other things) to find a class which has been used to instantiate any object
class Classy:
pass
print(Classy.__name__)
# Classy
obj = Classy()
print(type(obj))
# <class '__main__.Classy'>
print(type(obj).__name__)
# Classy
class Snake:
pass
class Python(Snake):
pass
print(Python.__name__, 'is a', Snake.__name__)
# Python is a Snake
## __MODULE__
# stores the name of the module which contains the definition of the class
class Classy:
pass
print(Classy.__module__)
# __main__ ==> any module named __main__ is actually not a module, but the file currently being run
obj = Classy()
print(obj.__module__)
# __main__
## __BASES__
# __bases__ is a tuple. The tuple contains classes (not class names) which are direct
# superclasses for the class
# The order is the same as that used inside the class definition
class SuperOne:
pass
class SuperTwo:
pass
class Sub(SuperOne, SuperTwo):
pass
def printBases(cls): # un peu artificiel le résultat mais on comprend le principe
print('( ', end='')
for x in cls.__bases__:
print(x.__name__, end=' ')
print(')')
printBases(SuperOne)
# ( object )
printBases(SuperTwo)
# ( object )
printBases(Sub)
# ( SuperOne SuperTwo )
class Snake:
pass
class Python(Snake):
pass
print(Python.__bases__[0].__name__, 'can be', Python.__name__)
# Snake can be Python
## BAD PRACTICE ==> creer des variables d'instance du meme nom que des variables de class
# ci-dessous un exemple du fonctionnement
class Demo:
class_var = 'shared variable'
d1 = Demo()
d3 = d1
d2 = Demo()
# both instances allow access to the class variable
print(d1.class_var)
# shared variable
print(d2.class_var)
# shared variable
# d1 object has no instance variable
print('contents of d1:', d1.__dict__)
# contents of d1: {}
# d1 object receives an instance variable named 'class_var'
d1.class_var = "I'm messing with the class variable"
# d1 object owns the variable named 'class_var' which holds a different value than the class variable named in the same way
print('contents of d1:', d1.__dict__)
# contents of d1: {'class_var': "I'm messing with the class variable"}
print(d1.class_var)
# I'm messing with the class variable
# d3 object is linked to d1 so has the same value
print(d3.class_var)
# I'm messing with the class variable
# d2 object variables were not influenced
print('contents of d2:', d2.__dict__)
# contents of d2: {}
# d2 object variables were not influenced
print('contents of class variable accessed via d2:', d2.class_var)
# contents of class variable accessed via d2: shared variable
# Bien comprendre l'interaction sur les variables de Class, selon qu'elle soient modifiées via l'Objet ou la Class
class BluePrint:
__element = 1
def __init__(self):
self.component = 1
def __action(self):
pass
product = BluePrint()
print(product.__dict__)
# {'component': 1} # pas de __element, la variable de Class n'est PAS une variable d'Objet
#### ATTRIBUTE
# An attribute is a capacious term that can refer to two major kinds of class traits:
# - variables, containing information about the class itself or a class instance; classes and class instances can own many variables
# - methods, formulated as Python functions; they represent a behavior that could be applied to the object
# It is said that methods are the 'callable attributes' of Python objects
class Person:
species = "Homo sapiens" # Variable de classe
def __init__(self, name, age): # Method dite constructor
self.name = name # Attribut d'instance
self.age = age # Attribut d'instance
self.__secret = "secret_value" # Attribut privé d'instance - ENCAPSULATION
def greet(self): # Méthode d'instance
return f"Bonjour, je m'appelle {self.name}"
# attributes for classes VS for instances:
class Person:
species = "Homo sapiens" # Attribut de classe
def __init__(self, name, age): # ==> = __static_attributes__
self.name = name # Attribut d'instance
self.age = age # Attribut d'instance
self.__secret = "secret_value" # Attribut privé d'instance - ENCAPSULATION
def greet(self): # Méthode d'instance
return f"Bonjour, je m'appelle {self.name}"
p = Person("Alice", 30) # Création d’une instance (objet) de la class Person
# ==> Attributs de la classe
print(Person.__dict__) # Dictionnaire des attributs de la classe Person()
# {'__module__': '__main__', '__firstlineno__': 48, 'species': 'Homo sapiens', '__init__': <function Person.__init__ at 0x000002047D312020>,
# 'greet': <function Person.greet at 0x000002047D3120C0>, '__static_attributes__': ('__secret', 'age', 'name'),
# '__dict__': <attribute '__dict__' of 'Person' objects>,
# '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
# on voit bien: varible de Class:species, functions:__init__, greet + __static_attributes__: '__secret', 'age', 'name'
# ==> Attributs de l’objet
print(p.__dict__) # Dictionnaire des attributs de l’instance p(Person)
# {'name': 'Alice', 'age': 30, '_Person__secret': 'secret_value'}
# on voit bien: name et age qui sont attendus comme argument pour créer l'instance + _Person__secret qui est "encapsulée" (cachée)
# Accès aux attributs de Class
print("Classe :", Person.species)
# Classe : Homo sapiens
print("Objet :", p.species) # hérité de la class
# Objet : Homo sapiens
# Accès à l'attribut privé (name mangling) de la class
print(Person._Person__odor)
# stinky
# Accès aux attributs d'instance
print("Nom de l'objet :", p.name)
# Nom de l'objet : Alice
print("Age de l'objet :", p.age)
# Age de l'objet : 30
# Accès à l'attribut privé (name mangling) de l'instance
print(p._Person__secret)
# secret_value
#### ENCAPSULATION
# __ = private !
# L'encapsulation en Python (et en programmation orientée objet en général) désigne le fait de cacher certains détails internes d'une class
# pour éviter qu'ils soient modifiés ou utilisés de manière inappropriée directement depuis l'extérieur
# Cela permet de protéger l'intégrité des données et de contrôler l'accès à celles-ci via des méthods (getter, setter)
# En Python, cette notion est basée sur la convention plutôt que sur une restriction stricte, contrairement à d'autres langages (comme Java)
# Différents types d'encapsulation en Python
# | Syntaxe | Signification | Description
# |-----------------|--------------------------------------|----------------------------------------------------------------------------
# | _attribut | **Protégé** (convention) | Indique que l'attribut est destiné à un usage interne
# | __attribut | **Privé** (manglé, name mangling) | Renomme l'attribut pour le rendre difficile d'accès direct depuis l'extérieur
class Stack:
def __init__(self):
self.__stack_list = []
stack_object = Stack()
print(len(stack_object.__stack_list))
# AttributeError: 'Stack' object has no attribute '__stack_list' ==> on voit bien que l'on ne peut pas accéder "simplement" à l'attribut privé
print(len(stack_object._Stack__stack_list))
# 0
##### DEEP copy
import copy
class Example:
def __init__(self): # __init__() method is executed only once, despite the fact we have two instances of the Example class
self.properties = ["112", "997"]
print("Hello from __init__()")
a_example = Example()
# Hello from __init__()
b_example = copy.deepcopy(a_example)
print("Memory chunks:", id(a_example), id(b_example))
# Memory chunks: 2098880150704 2098883041616
print("Same memory chunk?", a_example is b_example)
# Same memory chunk? False
b_example.properties.append("911")
print('a_example.properties:', a_example.properties)
# a_example.properties: ['112', '997']
print('b_example.properties:', b_example.properties)
# b_example.properties: ['112', '997', '911']