CLASS AND OBJECT
DATA SCIENCE
CONTENTS
Introduction
Class
Object
Constructor and Destructor
Advantages and Disadvantages
Conclusion
Introduction
Python is an “object-oriented programming language.” This means that
almost all the code is implemented using a special construct called
classes. Programmers use classes to keep related things together. This is
done using the keyword “class,” which is a grouping of object-oriented
constructs.
A class is a code template for creating objects. Objects have member
variables and have behaviour associated with them. In python a class is
created by the keyword class. An object is created using the constructor
of the class. This object will then be called the instance of the class.
Class
A class is like a object constructor or a blue print for creating objects.
User interphase of object is called Class.
Classes will have attributes (features) and then the methods (behaviour).
Example:
Features Method
§ Model § Start
§ Engine type § Stop
§ Color § Gear change
§ Size
If we have to access attributes and methods of class we need create object.
The keyword class is used to create a class.
Syntax :
class classname :
statements
Object
It is the instance of a class.
The objective used to access the attributes and methods of class.
Object are always created using the class name.
An object is created outside a class.
Creating Object :
obj = class_name()
Example :
Constructor and Destructor
_ _init_ _(self) works when an object is created.
Constructors are generally used for initialisation of object.
Constructors initialise (assign values to) data members of the class when an
object is created.
_ _init_ _() method is the constructor in python and is always called when an
object is created.
Destructor is used to delete the object.
_ _del_ _(self) :
Constructor are of two types :
Non – parameterized constructor
Parameterized constructor
Non - parameterized constructor :
A non - parameterized constructor is the constructor that does not take any
arguments.
Example :
Parametrized constructor :
The parametrized constructor takes in the parameters and sets the value.
Example :
Advantages and Disadvantages
Advantages Disadvantages
Classes provides another Misuse of class-level attributes can
functionality of this object- lead to global state issues, making
oriented programming paradigm, it difficult to reason about the
that is, inheritance. behavior of code.
Classes also help in overriding any Object-oriented concepts,
standard operator. including classes, can be
challenging for beginners to grasp.
Provides the ability to reuse the
code, which makes the program Inheritance can lead to problems
more efficient. such as tight coupling, making the
code harder to maintain and test.
Conclusion
In conclusion, the concept of classes and objects is fundamental in
object-oriented programming. Classes serve as blueprints for creating
objects, which are instances of those classes. They encapsulate data and
behavior, promoting modularity and reusability in software development.
Understanding the relationship between classes and objects is essential
for building robust and maintainable code.
THANK YOU
ASWIN S