<jupyter_start><jupyter_text>Python Classes and Objects Python is an object oriented programming language.Almost everything in Python is an object, with its properties and methods.A Class is like an object constructor, or a "blueprint" for creating objects.  Create a ClassTo create a class, use the keyword class:<jupyter_code>class MyClass:
  x = 5<jupyter_output><empty_output><jupyter_text>Note:Class name is generally written in pascal case.  Create ObjectNow we can use the class named MyClass to create objects:<jupyter_code>p1 = MyClass()
print(p1.x)<jupyter_output>5<jupyter_text>The `__init__()` FunctionThe examples above are classes and objects in their simplest form, and are not really useful in real life applications.To understand the meaning of classes we have to understand the built-in `__init__()` function.All classes have a function called `__init__()`, which is always executed when the class is being initiated.Use the `__init__()` function to assign values to object properties, or other operations that are necessary to do when the object is being created:<jupyter_code>class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)<jupyter_output>John
36<jupyter_text>Note: The __init__() function is called automatically every time the class is being used to create a new object.  Object MethodsObjects can also contain methods. Methods in objects are functions that belong to the object. Let us create a method in the Person class:<jupyter_code>class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("John", 36)
print(p1.myfunc())<jupyter_output>Hello my name is John
None<jupyter_text>Note: The self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.  The self ParameterThe self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class:<jupyter_code>#Use the words mysillyobject and abc instead of self:
class Person:
  def __init__(mysillyobject, name, age):
    mysillyobject.name = name
    mysillyobject.age = age

  def myfunc(abc):
    print("Hello my name is " + abc.name)

p1 = Person("John", 36)
p1.myfunc()<jupyter_output>Hello my name is John<jupyter_text>Modify Object PropertiesYou can modify properties on objects like this:<jupyter_code>#Set the age of p1 to 40:
p1.age = 40
p1.age<jupyter_output><empty_output><jupyter_text>Delete Object PropertiesYou can delete properties on objects by using the del keyword:<jupyter_code>#Delete the age property from the p1 object:
del p1.age<jupyter_output><empty_output><jupyter_text>Delete ObjectsYou can delete objects by using the del keyword:<jupyter_code>del p1<jupyter_output><empty_output><jupyter_text>The pass Statementclass definitions cannot be empty, but if you for some reason have a class definition with no content, put in the pass statement to avoid getting an error.<jupyter_code>class Person:
  pass<jupyter_output><empty_output><jupyter_text>Python Inheritance  Python InheritanceInheritance allows us to define a class that inherits all the methods and properties from another class.Parent class is the class being inherited from, also called base class.Child class is the class that inherits from another class, also called derived class.  Create a Parent ClassAny class can be a parent class, so the syntax is the same as creating any other class:<jupyter_code>class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

#Use the Person class to create an object, and then execute the printname method:

x = Person("John", "Doe")
x.printname()<jupyter_output><NAME><jupyter_text>Create a Child ClassTo create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class:<jupyter_code>#Create a class named Student, which will inherit the properties and methods from the Person class:
class Student(Person):
  pass<jupyter_output><empty_output><jupyter_text>Note: Use the pass keyword when you do not want to add any other properties or methods to the class. Now the Student class has the same properties and methods as the Person class.<jupyter_code>#Use the Student class to create an object, and then execute the printname method:
x = Student("Mike", "Olsen")
x.printname()<jupyter_output><NAME><jupyter_text>Add the `__init__()` FunctionSo far we have created a child class that inherits the properties and methods from its parent.We want to add the `__init__()` function to the child class (instead of the pass keyword). Note: The `__init__()` function is called automatically every time the class is being used to create a new object.<jupyter_code>#Add the __init__() function to the Student class:
class Student(Person):
  def __init__(self, fname, lname):
    '''add properties etc.'''<jupyter_output><empty_output><jupyter_text>When you add the `__init__()` function, the child class will no longer inherit the parent's `__init__()` function. Note: The child's `__init__()` function overrides the inheritance of the parent's `__init__()` function. To keep the inheritance of the parent's `__init__()` function, add a call to the parent's `__init__()` function:<jupyter_code>class Student(Person):
  def __init__(self, fname, lname):
    Person.__init__(self, fname, lname)<jupyter_output><empty_output><jupyter_text>Now we have successfully added the `__init__()` function, and kept the inheritance of the parent class, and we are ready to add functionality in the `__init__()` function.  Use the super() FunctionPython also has a super() function that will make the child class inherit all the methods and properties from its parent:<jupyter_code>class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)

class Person:
  country="India"
  def __init__(self):
    print("Initializing Person...\n")
  def takeBreath(self):
    print("I am breathing")
class Employee(Person):
  company="honda"
  def __init__(self):
    super().__init__()
    print("Initializing Employee...\n")
  def getSalary(self):
    print(f"Salary is {self.salary}")
  def takeBreath(self):
    super().takeBreath()
    print("I am a Employee so I am luckily breathing")
class Programmer(Employee):
  def __init__(self):
    # super().__init__()
    print("Initializing Programmer...\n")
  company="Fiverr"
  def getSalary(self):
    print(f"No salary to programmers")
  def takeBreath(self):
    super().takeBreath()
    print("I am a programmer so I am breathing++")
# p=Person()
# p.takeBreath()

# e=Employee()
# e.takeBreath()

pr=Programmer()
# pr.takeBreath()<jupyter_output>Initializing Programmer...<jupyter_text>By using the super() function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent.  Add Properties<jupyter_code>#Add a property called graduationyear to the Student class:

class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)
    self.graduationyear = 2019<jupyter_output><empty_output><jupyter_text>In the example below, the year 2019 should be a variable, and passed into the Student class when creating student objects. To do so, add another parameter in the `__init__()` function:<jupyter_code>class Student(Person):
  def __init__(self, fname, lname, year):
    super().__init__(fname, lname)
    self.graduationyear = year

x = Student("Mike", "Olsen", 2019)
print(x.graduationyear)<jupyter_output>2019<jupyter_text>Add Methods Add a method called welcome to the Student class:<jupyter_code>class Student(Person):
  def __init__(self, fname, lname, year):
    super().__init__(fname, lname)
    self.graduationyear = year

  def welcome(self):
    print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)
x = Student("Mike", "Olsen", 2019)
x.welcome()<jupyter_output>Welcome Mike Olsen to the class of 2019<jupyter_text>If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden.  Type of Inheritance:There are 3 types of inheritance in python as follows:1. Single inheritance2. Multiple inheritance3. Multilevel inheritance   Single InheritanceSingle inheritance occurs when child class inherits only a single parent class . ![single-inheritance](https://user-images.githubusercontent.com/72285021/120967743-815f0d00-c785-11eb-9c2b-7fc54ef975a5.png)<jupyter_code>class Employee:
  company="Google"
  def showDetails(self):
    print("This is an employee")
class Programmer(Employee):
  language="Python"
  #company="youtube"
  def getLanguage(self):
    print(f"The language is {self.language}")
  def showDetails(self):
    print("This is an programmer")
e=Employee()
e.showDetails()
p=Programmer()
p.showDetails()
print(p.company)<jupyter_output>This is an employee
This is an programmer
Google<jupyter_text>Multiple inheritance Multiple inheritance occurs when child class inherits only a single parent class .![download](https://user-images.githubusercontent.com/72285021/120967985-ddc22c80-c785-11eb-9417-a13729291143.png)<jupyter_code>class Employee:
  company="Visa"
  eCode=120
class Freelancer:
  company="Fiverr"
  level=0
  def upgradeLevel(self):
    self.level=self.level+1
class Programmer(Employee,Freelancer):
  name="Rohit"
p=Programmer()
p.upgradeLevel()
print(p.level)
print(p.company)<jupyter_output>1
Visa<jupyter_text>Multilevel inheritance:When a child class become a parent for another child class . ![download (1)](https://user-images.githubusercontent.com/72285021/120968212-31cd1100-c786-11eb-9371-89a50d65e355.png)<jupyter_code>class Person:
  country="India"
  def takeBreath(self):
    print("I am breathing")
class Employee(Person):
  company="honda"
  def getSalary(self):
    print(f"Salary is {self.salary}")
  def takeBreath(self):
    print("I am a Employee so I am luckily breathing")
class Programmer(Employee):
  company="Fiverr"
  def getSalary(self):
    print(f"No salary to programmers")
  def takeBreath(self):
    print("I am a programmer so I am breathing++")
p=Person()
p.takeBreath()
#print(p.company)#Throws an error 
e=Employee()
e.takeBreath()
print(e.company)
pr=Programmer()
pr.takeBreath()
print(pr.company)
print(pr.country)<jupyter_output>I am breathing
I am a Employee so I am luckily breathing
honda
I am a programmer so I am breathing++
Fiverr
India<jupyter_text>Class methods A class method is a method which is bound to the class and not the object of the class . `@classmethod` decorator is used to create a class method<jupyter_code>class Employee:
  company="camel"
  salary=100
  location="Delhi"
  # def changeSalary(self,sal):
  #   self.__class__.salary=sal 
  @classmethod
  def changeSalary(cls,sal):
    cls.salary=sal
e=Employee()
print(e.salary)
e.changeSalary(455)
print(e.salary)
print(Employee.salary)<jupyter_output>100
455
455<jupyter_text>Property Decorators<jupyter_code>class Employee:
  company="Bharat Gas"
  salary=5600
  salarybonus=400
  # totalSalary=6100
  @property
  def totalSalary(self):
    return self.salary+self.salarybonus
  @totalSalary.setter
  def totalSalary(self,val):
    self.salarybonus=val-self.salary
e=Employee()
print(e.totalSalary)
e.totalSalary=5800
print(e.salary)
print(e.salarybonus)<jupyter_output>6000
5600
200<jupyter_text>Operator overloading in pythonoperators in python can be overloaded using dunder methods.These methods are called when a giving operator is used on the objects.<jupyter_code>class Number:
  def __init__(self,num):
    self.num=num
  def __add__(self,num2):
    print("Lets add")
    return self.num+num2.num
  def __mul__(self,num2):
    print("Lets Multiply")
    return self.num*num2.num
n1=Number(4)
n2=Number(6)
mul=n1*n2
sum=n1+n2
print(sum)
print(mul)<jupyter_output>Lets Multiply
Lets add
10
24
{'fraction_non_alphanumeric': 0.09155388879830426, 'fraction_numerical': 0.01956628077612914, 'mean_word_length': 5.4495268138801265, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 84, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '24', 'dir': 'jupyter-structured-clean-dedup', 'id': '273875', 'license': 'Apache-2.0', 'n_tokens_mistral': 4026, 'n_tokens_neox': 3768, 'n_words': 1328, 'path': '09_object_oriented_programming.ipynb', 'repo_name': 'Shahid-coder/python-colab', 'size': '12272'}