Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Classes and Objects - v4

adsadwdasdsdw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Classes and Objects - v4

adsadwdasdsdw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

AI VIETNAM

All-in-One Course

Classes and Objects

TA. Khanh Duong

1
Year 2024
AI VIETNAM
All-in-One Course
Objectives

Classes and Objects Inheritance

Ø Class diagram Ø Definition and syntax


Ø Syntax for creating a class and objects Ø Access modifiers
Ø Constructor __init__ Ø Override

Ø self keyword Ø Types of inheritance


Ø Special method __call__
Ø Naming convention
Ø Other ways to use class
2
Outline
Ø Motivation
Ø Classes and Objects
Ø Inheritance

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

Robot Class Diagram


Robot

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

Class name Rectangle

+ 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

Class name Rectangle

+ 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

Ø 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

12
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.

13
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.

14
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?

15
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?

16
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.

17
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.

18
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.

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

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

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
24
AI VIETNAM
All-in-One Course
Classes and Objects

Fun fact:
In Python, everything is an object.

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

Therefore, an object of this class can


be an attribute of another class.

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

Is sorting like list possible?


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

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

EggBeater Robot VacuumCleaner


31
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.
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

Math1: super class or


parent class
+ is_even( )
+ factorial( )
Math2: child class or
derived class

Child classes can use


Math2
the public and protected
attributes and methods
of the super classes.
+ estimate_euler( )

52
53
AI VIETNAM
All-in-One Course
Summary

Classes and Objects Inheritance

ü Class diagram ü Definition and syntax


ü Syntax for creating a class and objects ü Access modifiers
ü Constructor __init__ ü Override

ü self keyword ü Types of inheritance


ü Special method __call__
ü Naming convention
ü Other ways to use class
54
AI VIETNAM
All-in-One Course

55

You might also like