Skip to main content

Objects and Classes (Basic OOP)

  • every object has a type, an internal data representation (a blueprint), a set of procedures for interacting with the object (method)
  • An object is an instance of a particular type
  • e.g Type 1: Object 1, Object 2,...

Objects​

Types​

  • using type() to find the type of an object
print(type(1))
Output
<class 'int'>
print(type('kevin'))
Output
<class 'str'>

Methods​

  • for example: .sort(), .append(), .add()

Classes​

Defining classes​

  • Class includes both Data attributes & Methods
  • You can create classes, for examples:

Class Car​

-> Data attributes: brand, color

class Car: # define your class

def __init__(self, brand, color): # self is self parameter
# brand and color are parameters
self.brand = brand
self.color = color
# those are data attributes used to initialize each instance of the class

my_car = Car('VinFast', 'red')
print(my_car.brand, my_car.color)
Output
VinFast red

Create an instance of a Class: Car​

class Car(object): # (object) is optional, in Python 2 we don't need (object)
def __init__(self, brand, color):
self.brand = brand
self.color = color

def drive(self):
print(f"The {self.color} {self.brand} is driving")

VinFast = Car('VinFast', 'red')

VinFast.drive()
Output
The red VinFast is driving
# NO __init__
class ToanTinTCTA:
pass
# this class is completely empty, no automatically assign a value or anything

# So now, if we want to create 2 individuals:
person1 = ToanTinTCTA()
person2 = ToanTinTCTA()
# U have to manually assign values to them, cuz its not automatic
person1.name = "Hong Phuc"
person1.username = "kevinph4n"

person2.name = "Hung Dan"
person2.username = "hungdan"

print(person1.name)
Output
Hong Phuc
# WITH __init__
class ToanTinTCTA:
def __init__(self, name, username):
self.name = name
self.username = username

person1 = ToanTinTCTA("Hong Phuc", "kevinph4n")
person2 = ToanTinTCTA("Hung Dan", "hungdan")

print(person1.name)
Output
Hong Phuc

More examples (OOP Intermediate)​

# Class
class WorldCup:
pass

print(WorldCup)
Output
<class '__main__.WorldCup'>
# Object
class WorldCup:
pass

Messi = WorldCup()
print(type(Messi).__name__)
Output
WorldCup
# Variables bound to an object
class WorldCup_0:
def __init__(self, full_name, won_the_wc, has_a_wife):
self.full_name = full_name
self.won_the_wc = won_the_wc
self.has_a_wife = has_a_wife

messi = WorldCup_0("Lionel Messi", "Yes", "1 Wife")
print(messi.full_name)
print(messi.won_the_wc)
print(messi.has_a_wife)
Output
Lionel Messi
Yes
1 Wife
class WorldCup_1:
def trash_talk(self): # Method
print("I have money!")

messi = WorldCup_1()
messi.trash_talk()
Output
I have money!
# Inheritance: Child class inherits from parent class

class Athlete:
pass

class Fighter(Athlete): # Inheritance
pass

print(issubclass(Fighter, Athlete))
Output
True
# Example using inheritance

class WorldCup_2:

def height(self, height):
# Maps height to height class
return "Short"

# Footballer class inherits from WorldCup_2
class Footballer(WorldCup_2):

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

def print_name(self):
print(self.name)

footballer = Footballer("Messi")
print(footballer.height(160))
footballer.print_name()
Output
Short
Messi
class student:
def __init__(self, name, age, GPA):
self.GPA = GPA
self.name = name
self.age = age

HungDan = student("Hung Dan", 21, 4.0)
HongPhuc = student("Hong Phuc", 18, 4.0)

print("HongPhuc's age: ", HongPhuc.age)
print("HungDan's GPA: ", HungDan.GPA)
Output
HongPhuc's age: 18
HungDan's GPA: 4.0