0% found this document useful (0 votes)
5 views32 pages

Section 3 - Ai201

This document provides an introduction to classes and objects in Python, explaining their definitions, creation, and manipulation. It covers accessing attributes, garbage collection, data hiding, and constructors, along with examples and tasks for practical application. Additionally, it includes a task to develop a Tic-Tac-Toe game using classes.

Uploaded by

youssefstudy01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views32 pages

Section 3 - Ai201

This document provides an introduction to classes and objects in Python, explaining their definitions, creation, and manipulation. It covers accessing attributes, garbage collection, data hiding, and constructors, along with examples and tasks for practical application. Additionally, it includes a task to develop a Tic-Tac-Toe game using classes.

Uploaded by

youssefstudy01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Ai201:

Introduction to
AI
Section Three
What is a Class in Python?
• In Python, a class is a user defined entity (data types) that
defines the type of data an object can contain and the
actions it can perform.
• It is used as a template for creating objects.
Creating Classes in Python
Example
What is an Object?
• An object is referred to as an instance of a given
Python class.
• Each object has its own attributes and methods,
which are defined by its class.
• When a class is created, it only describes the
structure of objects.
• The memory is allocated when an object is
instantiated from a class.
Creating Objects of Classes in Python
Accessing Attributes of Objects in
Python
You can add, remove, or modify attributes of
classes and objects at any time
Instead of using the normal statements to
access attributes, you can also use the
following functions
• getattr(obj, name[, default]) − to access the attribute of
object.
• hasattr(obj,name) − to check if an attribute exists or not.
• setattr(obj,name,value) − to set an attribute. If attribute
does not exist, then it would be created.
• delattr(obj, name) − to delete an attribute.
Example
Built-In Class Attributes in Python
Example
For the above Employee class, let us try to access its attributes :
Garbage Collection(Destroying
Objects) in Python

• Python deletes unwanted objects (built-in types or


class instances) automatically to free the memory
space.
• The process by which Python periodically reclaims
blocks of memory that no longer are in use is
termed Garbage Collection.
Example
• The __del__() destructor prints the class name of an instance that is
about to be destroyed as shown in the below code block:
Data Hiding in Python

• Python protects those members by internally


changing the name to include the class name.
• You can access such attributes
as object._className__attrName.
• If you would replace your last line as following,
then it works for you
Python Constructors
Default Constructor in Python
Parameterized Constructor
Python Multiple Constructors
For more about Classes and Objects
visit ➔
https://www.tutorialspoint.com/python/
python_constructors.htm
Rolling Die Python Code Using
Random

Your Task➔ Develop class


die that gives you the same
results without using random
Tic-Tac-Toe Game Python Code
Tic-Tac-Toe Game Python Code (cont..)
Tic-Tac-Toe Game Python Code (cont..)
Tic-Tac-Toe Game Python Code (cont..)
Tic-Tac-Toe Game Python Code (cont..)
Tic-Tac-Toe Game Python Code (cont..)
Tic-Tac-Toe Game Python Code (cont..)
Tic-Tac-Toe Game Python Code (cont..)
Tic-Tac-Toe Game Python Code (cont..)

Your Task➔ Develop Tic-Tac-Toe Game using class


The end

You might also like