PYTHON - OOP(OBJECT ORIENTED PROGRAMMING) - PART 1
OOP IN PYTHON
-- To map with real world scenarios, we started using objects in code. This is called Object Oriented Programming.
-- Utilizing functions reduces redundancy and enhances code reusability.
CLASS AND OBJECT IN PYTHON
-- Class is a blueprint which tells us that what features should a class have.
-- All the information which we have to store in particular object and writing in a class is what we call blueprint for creating an object
# First we have to define class
* Creating class
class Student: (Remember you always have to start class's name by capital letter)
name = "Anshula"
* After creating class, we need to create object
Eg:
s1 = Student ()
print(s1.name) (object and instances of class is same)
** Suppose there is a factory and you are required to create a class.
class Car:
color = "blue"
brand = "mercedes"
car1 = Car() #creating object
print(car1.color)
** __init__ FUNCTION
All classes have a function called __init__(), which is always executed when object is being initiated.
[init is short for initialising]
#Creating class #Creating object
class Student: s1 = Student("Anshula")
def__init__(self, fullname): print(s1.name)
self.name = fullname
-- The self parameter is a reference to the current instance of class and is used to access variables that belongs to the class.
* CONSTRUCTOR
It means creating a new object in a class.
There are two types of constructor:
1. Default constructor-
It is a special method called __init__(self)
that initializes a new object's attributes when it is created. If you don't define an __init__
method in your class, Python provides a default constructor that doesn't perform any initialization.
2. Paramaterized constructor-
It is defined using the __init__
method, which accepts additional arguments beyond self
. These arguments are used to initialize the object's attributes with values provided at the time of object creation.
* What are attributes?
Attributes are variables that store data associated with an object or class. They define the state or properties of an object and can be categorized into two types:
"instance attributes and class attributes."
* Self is used when each data is different. Now, if students are of same college-we make a class attribute for common object.
For eg:
class Student:
college_name = "BCAS college"
name = 'anonymous' #class attribute
--When object attribute and class attribute has same value, object value is always prioritized.
#METHODS-
Methods are functions that belong to objects.
#Creating class #Creating object
class Student: s1 = Student( “Rahul” )
def__init__(self, fullname): s1.hello()
self.name = fullname
def hello(self):
print("hello", self.name)
-STATIC METHOD-
Methods that don't use the self parameter (work at class level)
class Student:
@staticmethod #decorator
def_college():
print("BCAS college")
**Decorators allow us to wrap another function in order to extend the behavior of wrapped function in order, without permanently modifying it.
Comments
Post a Comment