Python Object Oriented Programming Cheat Sheet Remarkable Secrets!
Complete Python Bootcamp From Zero to Hero in Python – Object Oriented Programming (OOP)
1.1 Objects
1.2 Class
1.3 Attributes
1.4 Methods
1.5 Inheritance
1.6 Polymorphism
1.7 Special Methods
1.1 Objects
print(type(1))
print(type(‘A’))
print(type([]))
print(type(()))
print(type({}))
# output:
<class ‘int’>
<class ‘str’>
<class ‘list’>
<class ‘tuple’>
<class ‘dict’>
—————————————————————–
1.2 Class – Python Object Oriented Programming Cheat Sheet Professional!
# Create a new object type called Sample
class Sample:
pass
# Instance of Sample
x = Sample()
print(type(x))
# output:
<class ‘__main__.Sample’>
—————————————————————–
1.3 Attributes
class Dog:
def __init__(self, breed):
self.breed = breed
kembot = Dog(breed=’Lab’)
kimchi = Dog(breed=’Huskie’)
hb = Dog(breed=’Frenchie’)
titan = Dog(breed=’Local’)
lucky = Dog(breed=’Askal’)
print(kembot.breed)
print(kimchi.breed)
print(hb.breed)
print(titan.breed)
print(lucky.breed)
# output:
Lab
Huskie
Frenchie
Local
Askal
—————————————————————–
class Dog:
# Class Object Attribute
species = ‘Mammal’
def __init__(self, breed, name):
self.breed = breed
self.name = name
dog_info = Dog(‘Lab’, ‘Kembot’)
print(dog_info.species)
print(dog_info.breed)
print(dog_info.name)
# output:
Mammal
Lab
Kembot
—————————————————————–
class Dog:
# Class Object Attribute
species = ‘Mammal’
def __init__(self, breed, name):
self.breed = breed
self.name = name
def bark(self, number):
print(“WOOF! My name is {}, my breed is {}, my number is {}”.format(self.name, self.breed, number))
dog_info = Dog(‘Lab’, ‘Kembot’)
print(dog_info.bark(11111111))
# output:
WOOF! My name is Kembot, my breed is Lab, my number is 11111111
—————————————————————–
1.4 Methods
class Circle:
pi = 3.14
# Circle gets instantiated with a radius (default is 1)
def __init__(self, radius=1):
self.radius = radius
self.area = radius * radius * Circle.pi
# Method for resetting Radius
def setRadius(self, new_radius):
self.radius = new_radius
self.area = new_radius * new_radius * self.pi
# Method for getting Circumference
def getCircumference(self):
return self.radius * self.pi * 2
c = Circle()
print(‘Radius is: ‘, c.radius)
print(‘Area is: ‘, c.area)
print(‘Circumference is: ‘, c.getCircumference())
# output:
Radius is: 1
Area is: 3.14
Circumference is: 6.28
—————————————————————–
c = Circle()
c.setRadius(2)
print(‘Radius is: ‘, c.radius)
print(‘Area is: ‘, c.area)
print(‘Circumference is: ‘, c.getCircumference())
# output:
Radius is: 2
Area is: 12.56
Circumference is: 12.56
—————————————————————–
1.5 Inheritance
class Animal:
def __init__(self):
print(“Animal created”)
def whoAmI(self):
print(“Animal”)
def eat(self):
print(“Eating”)
class Dog(Animal):
def __init__(self):
Animal.__init__(self)
print(“Dog created”)
def whoAmI(self):
print(“Dog”)
def bark(self):
print(“Woof!”)
d = Dog()
# output:
Animal created
Dog created
—————————————————————–
d.whoAmI()
d.eat()
d.bark()
# output:
Dog
Eating
Woof!
—————————————————————–
1.6 Polymorphism
class Dog:
def __init__(self, name):
self.name = name
def speak(self):
return self.name + ‘ says Woof!’
class Cat:
def __init__(self, name):
self.name = name
def speak(self):
return self.name + ‘ says Meow!’
barney = Dog(‘Barney’)
tom = Cat(‘Tom’)
print(barney.speak())
print(tom.speak())
# output:
# Barney says Woof!
# Tom says Meow!
—————————————————————–
for pet in [barney, tom]:
print(pet.speak())
# output:
# Barney says Woof!
# Tom says Meow!
—————————————————————–
def pet_speak(pet):
print(pet.speak())
pet_speak(barney)
pet_speak(tom)
# output:
# Barney says Woof!
# Tom says Meow!
—————————————————————–
class Animal:
def __init__(self, name): # Constructor of the class
self.name = name
def speak(self): # Abstract method, defined by convention only
raise NotImplementedError(“Subclass must implement abstract method”)
class Dog(Animal):
def speak(self):
return self.name + ‘ says Woof!’
class Cat(Animal):
def speak(self):
return self.name + ‘ says Meow!’
barney = Dog(‘Barney’)
tom = Cat(‘Tom’)
print(barney.speak())
print(tom.speak())
# output:
Barney says Woof!
Tom says Meow!
—————————————————————–
1.7 Special Methods – Python Object Oriented Programming Cheat Sheet Best Selling!
The __init__(), __str__(), __len__() and __del__() special methods
class Book:
def __init__(self, title, author, pages):
print(“A book is created”)
self.title = title
self.author = author
self.pages = pages
def __str__(self):
return “Title: %s, author: %s, pages: %s” % (self.title, self.author, self.pages)
def __len__(self):
return self.pages
def __del__(self):
print(“A book is deleted”)
book = Book(“Python – Smarter not Harder”, “Al Ardosa”, 100)
# Special Methods
print(book)
print(len(book))
del book
# output:
A book is created
Title: Python – Smarter not Harder, author: Al Ardosa, pages: 100
100
A book is deleted