38
loading...
This website collects cookies to deliver better user experience
The scope of this article is to help you understand the concepts of oops quickly and not an in-depth tutorial on coding with oops.
encapsulation (the action of enclosing something in or as if in a capsule.)
class Cat:
def __init__(self):
self.sound = "meow"
def speak(self):
print("Cat says: {}".format(self.sound))
c = Cat()
c.speak()
# change the price
c.sound = "bow-wow"
c.speak()
#OUTPUT
Cat says: meow
Cat says: bow-wow
c.sound = "bow-wow"
, Now that you know the meaning of encapsulation as per above definition let's consider class as a capsule, do you think this code follows encapsulation? Well, it doesn't.class Cat:
def __init__(self):
self.__sound = "meow"
def speak(self):
print("Cat says: {}".format(self.__sound))
c = Cat()
c.speak()
# change the price
c.sound = "bow-wow"
c.speak()
#OUTPUT
Cat says: meow
Cat says: meow
Inherit (to receive a quality, characteristic, etc. from your parents or family.)
class Family:
def __init__(self, family_name, number_of_members, country):
self.family_name = family_name
self.number_of_members = number_of_members
self.country=country
def member_says(self):
print(f"Hey, I am from {self.family_name} family and there are {self.number_of_members} members in family")
class Family_detailed:
def __init__(self, family_name, number_of_members, country):
self.family_name = family_name
self.number_of_members = number_of_members
self.country=country
def member_says(self):
print(f"Hey, I am from {self.family_name} family and there are {self.number_of_members} members in family")
def which_country(self):
print(f"The {self.family_name} family has roots from {self.country}" )
a = Family("Rodrigues",5,"Peru")
b = Family_detailed("Bezos",15,"United States of America")
a.member_says()
b.member_says()
b.which_country()
#Output
Hey, I am from the Rodrigues family and there are 5 members in the family
Hey, I am from the Bezos family and there are 15 members in the family
The Bezos family has roots in the United States of America
class Family:
def __init__(self, family_name, number_of_members, country):
self.family_name = family_name
self.number_of_members = number_of_members
self.country=country
def member_says(self):
print(f"Hey, I am from {self.family_name} family and there are {self.number_of_members} members in family")
class Family_detailed(Family):
def which_country(self):
print(f"The {self.family_name} family has roots from {self.country}" )
a = Family("Rodrigues",5,"Peru")
b = Family_detailed("Bezos",15,"United States of America")
a.member_says()
b.member_says()
b.which_country()
#Output
Hey, I am from the Rodrigues family and there are 5 members in the family
Hey, I am from the Bezos family and there are 15 members in the family
The Bezos family has roots in the United States of America
from abc import ABC, abstractmethod
class Company(ABC):
def work(self):
pass
class Manager(Company):
def work(self):
print("I assign work to and manage team")
class Employee(Company):
def work(self):
print("I complete the work assigned to me")
# Driver code
R = Manager()
R.work()
K = Employee()
K.work()
class Class1():
def pt(self):
print("This function determines class 1")
class Class2():
def pt(self):
print("This function determines class 2.")
obj1 = Class1()
obj2 = Class2()
for type in (obj1, obj2): # creating a loop to iterate through the obj1, obj2
type.pt()
#Output
This function determines class 1
This function determines class 2.
+
operator.> print(1+2)
3
> print ("a"+"b")
> ab
+
operator behaves different way with different datatypes.