Show Menu
Cheatography

Python Basics - Object Oriented Programming Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Class - Syntax

class Dog(object):
    living = True
    age = 0

    def __init__(self, name, sex):
        self.name = name
        self.sex = sex

    def bark(self):
        print(f"{self.name} barked!")

luke = Dog('Luke', 'Male')

print(f"Name: {luke.name}")
print(f"Sex: {luke.sex}")
print(f"Living: {luke.living}")
print(f"Age: {luke.age}")
luke.bark()
Name: Luke
Sex: Male
Living: True
Age: 0
Luke barked!

Class - Magic Methods

__init­__(­self, other)
Overrides object creation method
__repr­__(­self)
Overrides object string repres­ent­ation
__add_­_(self, other)
Overrides + operator
__sub_­_(self, other)
Overrides - operator
__mul_­_(self, other)
Overrides * operator
__floo­rdi­v__­(self, other)
Overrides // operator
__true­div­__(­self, other)
Overrides / operator
__mod_­_(self, other)
Overrides % operator
__pow_­_(self, other[, modulo])
Overrides ** operator
__lt__­(self, other)
Overrides < comparison operator
__le__­(self, other)
Overrides <= comparison operator
__eq__­(self, other)
Overrides == comparison operator
__ne__­(self, other)
Overrides != comparison operator
__ge__­(self, other)
Overrides >= comparison operator
__gt__­(self, other)
Overrides > comparison operator
__call­__(­self[, args...])
Overrides ( ) operator
__int_­_(self)
Overrides int() method
__floa­t__­(self)
Overrides float() method
__str_­_(self)
Overrides str() method
__abs_­_(self)
Overrides abs() method
__len_­_(self)
Overrides len() method
__cont­ain­s__­(self, item)
Overrides in keyword behavior

OOP - Inheri­tance

class Vehicle(object):
    def honk(self):
        print('Honk honk')

class Car(Vehicle):
    def accelerate(self):
        print('Vrooom')

honda = Car()
honda.honk()
honda.accelerate()
Honk honk
Vrooom