Classes and Objects

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

AI VIETNAM

All-in-One Course

Classes and Objects

TA. Khanh Duong

1
Year 2024
Outline
Ø Motivation
Ø Classes and Objects
Ø Inheritance

2
AI VIETNAM
All-in-One Course
Motivation
v Review procedural programming

ML Robot

CV Robot

NLP Robot

3
AI VIETNAM
All-in-One Course
Motivation
v Object-based Organization

ML Robot

CV Robot

NLP Robot

4
AI VIETNAM
All-in-One Course
Motivation
v Group the common features of the objects into a prototype

5
AI VIETNAM
All-in-One Course
Motivation
v Use the prototype instead of objects

ML Robot

CV Robot

NLP Robot

6
Outline
Ø Motivation
Ø Classes and Objects
Ø Inheritance

7
AI VIETNAM
All-in-One Course
Classes and Objects
v Abstract view

Robot Class Diagram


Robot

Methods/ head
Attributes
Actions body
arm
leg
move
process
head body arm leg move process emit
emit

8
AI VIETNAM
All-in-One Course
Classes and Objects
v Class Diagram

Class name Rectangle

+ width
Attributes + height 3 parts

+ calculate_area ( )
Methods
+ calculate_perimeter ( )

9
AI VIETNAM
All-in-One Course
Classes and Objects
v Syntax for creating a class

Class name Rectangle

+ width
Attributes + height

+ calculate_area ( )
Methods + calculate_perimeter ( )

10
AI VIETNAM
All-in-One Course
Classes and Objects
v Syntax for creating a class
Class

Ø A class is a template for creating object.


Ø It is possible to create multiple objects
from one class.

Ø An object is an instance of a class.


Ø Another term for object is instance.

Objects

11
AI VIETNAM
All-in-One Course
Classes and Objects
v Constructor

The __init__() function is called


automatically every time the class
is being used to create a new object.

The __init__() method is used to


initialize the attributes of the object
with specific values.

12
AI VIETNAM
All-in-One Course
Classes and Objects
v Constructor

Note: Not all attributes have to be


initialized in the __init__() method.
Attributes can be created in other
methods.

13
AI VIETNAM
All-in-One Course
Classes and Objects
v Another approach to declaring a class

How to customize the


values of the constants in
the class?

14
AI VIETNAM
All-in-One Course
Classes and Objects
v Self keyword

What will happen if the __init__


function is used but the self keyword
is not?

Can all the variables that appear in the


class be considered attributes?

15
AI VIETNAM
All-in-One Course
Classes and Objects
v Self keyword

The self keyword is used to represent


the instance of the class.

Variables prefixed with self are the


attributes of the class, while others are
merely local variables of the class.

16
AI VIETNAM
All-in-One Course
Classes and Objects
v Some rules when using self keyword

The self keyword must always be the


first parameter in each method.

When invoking a method, it is not


necessary to pass the self variable.

17
AI VIETNAM
All-in-One Course
Classes and Objects
v Replacement for self keyword

Fun fact: We can certainly replace


self variable with another word.
Python automatically interprets the
first parameter of a method as the
instance variable.

18
AI VIETNAM
All-in-One Course
Classes and Objects
v The special function: __call__( ) method

__call__( ) function: instances behave


like functions and can be called like a
functions.

19
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
20
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 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

SuperCat For attribute names

+ cat_name Use nouns or noun phrases


+ cat_color
+ cat_age Words separated by
underscores
+ get_name( )
+ set_name( )
For method names
For class names
Prioritize using verbs or
Including words phrasal verbs
concatenated
Words separated by
Each word starts with
underscores
upper case
23
AI VIETNAM
All-in-One Course
Classes and Objects

Fun fact:
In Python, everything is an object.

24
AI VIETNAM
All-in-One Course
Classes and Objects

Therefore, an object of this class can


be an attribute of another class.

25
AI VIETNAM
All-in-One Course
Lists and Classes

Is sorting like list possible?


If so, what criteria will it sort by?
26
AI VIETNAM
All-in-One Course
Lists and Classes
Approach 1:

Approach 2:

27
Outline
Ø Motivation
Ø Classes and Objects
Ø Inheritance

28
AI VIETNAM
All-in-One Course
Inheritance
v Motivation

VacuumCleaner

EggBeater
29
AI VIETNAM
All-in-One Course
Inheritance
v Motivation
Chip

EggBeater Robot VacuumCleaner


30
AI VIETNAM
All-in-One Course
Inheritance
v Some benefits of Inheritance

Some benefits that Inheritance provides, similar to


using variables in coding.

Ø Code Reusability
Inheritance allows you to reuse previously written
code segments.

Ø Scalability
You can easily extend the functionality of classes by
modifying the SuperClass.
31
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

32
AI VIETNAM
All-in-One Course
Inheritance
v Example

Animal
+ name
+ make_sound( )

is

Cat
+ breed
+ info( )

33
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( )

34
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers: Public

Cat

+ name
+ color
+ age

//..

35
AI VIETNAM
All-in-One Course
Inheritance
v Access Modifiers: Private
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: Protected

Animal
+ name
# color
# make_sound( )

is

Cat
+ breed
+ info( )
+ sound( )

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 Override
Animal
+ name
+ make_sound( )

is

Cat
+ breed
+ info( )
+ make_sound( )

40
AI VIETNAM
All-in-One Course
Example
v Implement the two classes below

Math1 Math2

+ isEven(int): bool + isEven(int): bool


+ factorial(int): int + factorial(int): int
+ estimateE(int): double

41
AI VIETNAM
All-in-One Course
Example
v Implement the two classes below

Math1

+ isEven(int): bool
+ factorial(int): int

42
AI VIETNAM
All-in-One Course
Example
v Implement the two classes below

𝑒 = 2.71828

Math2

+ isEven(int): bool
+ factorial(int): int
+ estimateEuler(int): double

43
AI VIETNAM
All-in-One Course
Example
Implement the two classes below

𝑒 = 2.71828

Math2

+ isEven(int): bool
+ factorial(int): int
+ estimateEuler(int): double

44
AI VIETNAM
All-in-One Course
Example
How to reuse an existing class?
Math1

+ isEven(int): bool
+ factorial(int): int

Math2

+ isEven(int): bool
+ factorial(int): int
+ estimateEuler(int): double
45
AI VIETNAM
All-in-One Course
Example
v Inheritance
Math1

Math1: super class or


parent class
+ isEven(int): bool
+ factorial(int): int
Math2: child class or
derived class

Child classes can use


Math2
the public and protected
attributes and methods
of the super classes.
+ estimateEuler(int): double

46
47
AI VIETNAM
All-in-One Course

48

You might also like