Section 3 - Ai201
Section 3 - 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