Classes and Objects - v4
Classes and Objects - v4
All-in-One Course
1
Year 2024
AI VIETNAM
All-in-One Course
Objectives
3
AI VIETNAM
All-in-One Course
Motivation
v Review procedural programming
ML Robot
CV Robot
NLP Robot
4
AI VIETNAM
All-in-One Course
Motivation
v Object-based Organization
ML Robot
CV Robot
NLP Robot
5
AI VIETNAM
All-in-One Course
Motivation
v Group the common features of the objects into a prototype
6
AI VIETNAM
All-in-One Course
Motivation
v Use the prototype instead of objects
ML Robot
CV Robot
NLP Robot
7
Outline
Ø Motivation
Ø Classes and Objects
Ø Inheritance
8
AI VIETNAM
All-in-One Course
Classes and Objects
v Abstract view
Methods/ head
Attributes
Actions body
arm
leg
move
process
head body arm leg move process emit
emit
9
AI VIETNAM
All-in-One Course
Classes and Objects
v Class Diagram
+ width
Attributes + height 3 parts
+ calculate_area ( )
Methods
+ calculate_perimeter ( )
10
AI VIETNAM
All-in-One Course
Classes and Objects
v Syntax for creating a class
+ width
Attributes + height
+ calculate_area ( )
Methods + calculate_perimeter ( )
11
AI VIETNAM
All-in-One Course
Classes and Objects
v Syntax for creating a class
Class
Objects
12
AI VIETNAM
All-in-One Course
Classes and Objects
v Constructor
13
AI VIETNAM
All-in-One Course
Classes and Objects
v Constructor
14
AI VIETNAM
All-in-One Course
Classes and Objects
v Another approach to declaring a class
15
AI VIETNAM
All-in-One Course
Classes and Objects
v Self keyword
16
AI VIETNAM
All-in-One Course
Classes and Objects
v Self keyword
17
AI VIETNAM
All-in-One Course
Classes and Objects
v Some rules when using self keyword
18
AI VIETNAM
All-in-One Course
Classes and Objects
v Replacement for self keyword
19
AI VIETNAM
All-in-One Course
Classes and Objects
v The special function: __call__( ) method
20
AI VIETNAM
All-in-One Course
Naming Conventions
v Survey the naming styles of a few popular repos
https://github.com/ultralytics/yolov5/blob/master/models/yolo.py
21
AI VIETNAM
All-in-One Course
Naming Conventions
v Survey the naming styles of a few popular repos
https://github.com/lucidrains/denoising-diffusion-
pytorch/blob/main/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py 22
AI VIETNAM
All-in-One Course
Naming Conventions
v Survey the naming styles of a few popular repos
https://github.com/lucidrains/denoising-diffusion-
pytorch/blob/main/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py 23
AI VIETNAM
All-in-One Course
Naming Conventions
Fun fact:
In Python, everything is an object.
25
AI VIETNAM
All-in-One Course
Classes and Objects
26
AI VIETNAM
All-in-One Course
Lists and Classes
Approach 2:
28
Outline
Ø Motivation
Ø Classes and Objects
Ø Inheritance
29
AI VIETNAM
All-in-One Course
Inheritance
v Motivation
VacuumCleaner
EggBeater
30
AI VIETNAM
All-in-One Course
Inheritance
v Motivation
Chip
Ø Code Reusability
Inheritance allows you to reuse previously written
code segments.
Ø Scalability
You can easily extend the functionality of classes by
modifying the SuperClass.
32
AI VIETNAM
All-in-One Course
Inheritance
v Definition and simple syntax
SuperClass
Inheritance is a mechanism in object-
oriented programming (OOP) that allows a
new class to inherit the attributes and
methods of an existing class.
is
SubClass
33
AI VIETNAM
All-in-One Course
Inheritance
v Example
Animal
+ name
+ make_sound( )
is
Cat
+ breed
+ info( )
34
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers
Name Class
Ø Public data: Accessible anywhere from otside oclass.
+ public_attribute
Ø Private data: Accessible within the class # protected_attribute
Ø Protected data: Accessible within the class and its - private_attribute
sub-classes. + public_method( )
# protected_method( )
- private_method( )
35
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers: Public
Cat
+ name
+ color
+ age
//..
36
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers: Private
Cat
+ name
+ color
- age
//..
37
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers: Private
Cat
+ name
+ color
- age
//..
38
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers: Protected
Animal
+ name
# color
# make_sound( )
is
Cat
+ breed
+ info( )
+ sound( )
39
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers: Protected
Animal
+ name
# color
# make_sound( )
is
Cat
+ breed
+ info( )
+ sound( )
40
AI VIETNAM
All-in-One Course
Inheritance
v Override and extend
Animal
+ name
+ make_sound( )
is
Cat
+ breed
+ info( )
+ make_sound( )
41
AI VIETNAM
All-in-One Course
Inheritance
v Override and extend
Animal
+ name
+ make_sound( )
is
Cat
+ breed
+ info( )
+ make_sound( )
42
AI VIETNAM
All-in-One Course
Types of Inheritance
v Single Inheritance
Parent
+ name
+ display( )
Child
+ age
+ display( )
43
AI VIETNAM
All-in-One Course
Types of Inheritance
v Multiple Inheritance
Base1 Base2
+ str1 + str2
Derived
+ display( )
44
AI VIETNAM
All-in-One Course
Types of Inheritance
v Multilevel Inheritance
GrandParent
+ grandparent_name
+ display_grandparent( )
Parent
+ parent_name
+ display_parent( )
Child
+ child_name
+ display_child( ) 45
AI VIETNAM
All-in-One Course
Types of Inheritance
v Hierarchical Inheritance
Parent
+ name
+ display( )
Child1 Child2
+ age + grade
+ display( ) + display( )
46
AI VIETNAM
All-in-One Course
Example
v Implement the two classes below
Math1 Math2
+ is_even( ) + is_even( )
+ factorial( ) + factorial( )
+ estimate_euler( )
47
AI VIETNAM
All-in-One Course
Example
v Implement the two classes below
Math1
+ is_even( )
+ factorial( )
48
AI VIETNAM
All-in-One Course
Example
v Implement the two classes below
𝑒 = 2.71828
Math2
+ is_even( )
+ factorial( )
+ estimate_euler( )
49
AI VIETNAM
All-in-One Course
Example
Implement the two classes below
𝑒 = 2.71828
Math2
+ is_even( )
+ factorial( )
+ estimate_euler( )
50
AI VIETNAM
All-in-One Course
Example
How to reuse an existing class?
Math1
+ is_even( )
+ factorial( )
Math2
+ is_even( )
+ factorial( )
+ estimate_euler( )
51
AI VIETNAM
All-in-One Course
Example
v Inheritance
Math1
52
53
AI VIETNAM
All-in-One Course
Summary
55