CMPE-103-Module-4-Introduction-to-Object-oriented-Programming
CMPE-103-Module-4-Introduction-to-Object-oriented-Programming
OBJECT-ORIENTED PROGRAMMING
MODULE 4
OBJECT-ORIENTED PROGRAMMING
What is a Class and Objects in Python?
•Class: The class is a user-defined data structure that binds the data members and methods into a single unit. Class is a blueprint or
code template for object creation. Using a class, you can create as many objects as you want.
•Object: An object is an instance of a class. It is a collection of attributes (variables) and methods. We use the object of a class to
perform actions.
•State: An object has an attribute that represents a state of an object, and it also reflects the property of an object.
Python is an Object-Oriented Programming language, so everything in Python is treated as an object. An object is a real-life entity. It
is the collection of various data and functions that operate on those data.
Create a Class in Python
In Python, class is defined by using the class keyword. The syntax to create a class is given below.
Syntax:
class Person:
def __init__(self, name, sex, profession):
# data members (instance variables)
self.name = name
self.sex = sex
self.profession = profession
# call methods
jessa.show()
jessa.work()
Objects do not share instance attributes. Instead, every object
has its copy of the instance attribute and is unique to each
object.
All instances of a class share the class variables. However,
unlike instance variables, the value of a class variable is not
varied from object to object.
Only one copy of the static variable will be created and shared
between all objects of the class.
Accessing properties and assigning values
•An instance attribute can be accessed or modified by using the
dot notation: instance_name.attribute_name.
In Python, Object creation is divided into two parts in Object Creation and Object initialization
Default Constructor