From the Hiplisch (2018) Python for Finance 2nd ed:
Class An abstract definition of a certain type of objects. For example, a human being.
Object An instance of a class. For example, Sandra.
Attribute A feature of the class (class attribute) or of an instance of the class (instance attribute). For example, being a mammal, being male or female, or color of the eyes.
Method An operation that the class or an instance of the class can implement. For example, walking.
Parameters Input taken by a method to influence its behavior. For example, three steps.
Instantiation The process of creating a specific object based on an abstract class.
class Person:
class_attr = "I am a class attribute"
def __init__(self, name=''):
self.name = name
person_1 = Person('Laura')
person_1.name
'Laura'
person_1.class_attr
'I am a class attribute'
class Person:
class_attr = "I am a class attribute"
def __init__(self, name=''):
self.name = name
@property
def name(self):
return self.__name
@name.setter
def name(self, value):
if type(value) is not str:
raise TypeError("name must be a string")
self.__name = value
person_1 = Person('Laura')
print(person_1.name)
Laura
class HumanBeing(object):
def __init__(self, first_name, eye_color):
self.first_name = first_name
self.eye_color = eye_color
self.position = 0
def walk_steps(self, steps):
self.position += steps
From https://vegibit.com/python-class-examples/
class A user-defined compound type. A class can also be thought of as a template for the objects that are instances of it. (The iPhone is a class. By December 2010, estimates are that 50 million instances had been sold!)
class Point:
""" Create a new Point, at coordinates x, y """
def __init__(self, x=0, y=0):
""" Create a new point at x, y """
self.x = x
self.y = y
def addition(self):
""" Compute my distance from the origin """
return self.x + self.y
p = Point(1, 4)
print(p)
<__main__.Point object at 0x39385f8>
p.x
1
p.y
4
p.addition()
5
q = Point(100, 2**3)
q.addition()
108
a = p.addition()*2 + q.addition()
print(a)
118
class Vehicle:
def __init__(self, brand, model, type):
self.brand = brand
self.model = model
self.type = type
self.gas_tank_size = 14
self.fuel_level = 0
def fuel_up(self):
self.fuel_level = self.gas_tank_size
print('Gas tank is now full.')
def drive(self):
print(f'The {self.model} is now driving.')
vehicle_object = Vehicle('Honda', 'Ridgeline', 'Truck')
a_subaru = Vehicle('Subaru', 'Forester', 'Crossover')
an_suv = Vehicle('Ford', 'Explorer', 'SUV')
print(vehicle_object)
print(vehicle_object.brand)
print(vehicle_object.model)
print(vehicle_object.type)
vehicle_object.fuel_up()
vehicle_object.drive()
<__main__.Vehicle object at 0x7f23bf420bd0> Honda Ridgeline Truck Gas tank is now full. The Ridgeline is now driving.
cool_new_vehicle = Vehicle('Honda', 'Ridgeline', 'Truck')
cool_new_vehicle.fuel_level = 7
class Vehicle:
def __init__(self, brand, model, type):
self.brand = brand
self.model = model
self.type = type
self.gas_tank_size = 14
self.fuel_level = 0
def fuel_up(self):
self.fuel_level = self.gas_tank_size
print('Gas tank is now full.')
def drive(self):
print(f'The {self.model} is now driving.')
def update_fuel_level(self, new_level):
if new_level <= self.gas_tank_size:
self.fuel_level = new_level
else:
print('Exceeded capacity')
class Vehicle:
def __init__(self, brand, model, type):
self.brand = brand
self.model = model
self.type = type
self.gas_tank_size = 14
self.fuel_level = 0
def fuel_up(self):
self.fuel_level = self.gas_tank_size
print('Gas tank is now full.')
def drive(self):
print(f'The {self.model} is now driving.')
def update_fuel_level(self, new_level):
if new_level <= self.gas_tank_size:
self.fuel_level = new_level
else:
print('Exceeded capacity')
def get_gas(self, amount):
if (self.fuel_level + amount <= self.gas_tank_size):
self.fuel_level += amount
print('Added fuel.')
else:
print('The tank wont hold that much.')
class ElectricVehicle(Vehicle):
def __init__(self, brand, model, type):
super().__init__(brand, model, type)
self.battery_size = 85
self.charge_level = 0
class ElectricVehicle(Vehicle):
def __init__(self, brand, model, type):
super().__init__(brand, model, type)
self.battery_size = 85
self.charge_level = 0
def charge(self):
self.charge_level = 100
print('The vehicle is now charged.')
def fuel_up(self):
print('This vehicle has no fuel tank!')
electric_vehicle = ElectricVehicle('Tesla', 'Model 3', 'Car')
electric_vehicle.charge()
electric_vehicle.drive()
The vehicle is now charged. The Model 3 is now driving.
class ElectricVehicle(Vehicle):
def __init__(self, brand, model, type):
super().__init__(brand, model, type)
self.battery_size = 85
self.charge_level = 0
def charge(self):
self.charge_level = 100
print('The vehicle is now charged.')
def fuel_up(self):
print('This vehicle has no fuel tank!')
class Battery:
def __init__(self, size=85):
self.size = size
self.charge_level = 0
def get_range(self):
if self.size == 85:
return 260
elif self.size == 100:
return 315
class ElectricVehicle(Vehicle):
def __init__(self, brand, model, type):
super().__init__(brand, model, type)
self.battery = Battery()
def charge(self):
self.battery.charge_level = 100
print('The vehicle is fully charged.')
The vehicle is fully charged.
electric_vehicle = ElectricVehicle('Tesla', 'CyberTruck', 'Truck')
electric_vehicle.charge()
print(electric_vehicle.battery.get_range())
electric_vehicle.drive()
260 The CyberTruck is now driving.
class Vehicle:
"""Vehicle Class data and methods"""
class Battery:
"""Batter Class data and methods"""
class ElectricVehicle(Vehicle):
"""ElectricVehicle Class data and methods"""
class Shark:
def swim(self):
print("The shark is swimming.")
def be_awesome(self):
print("The shark is being awesome.")
sammy = Shark()
sammy = Shark()
sammy.swim()
sammy.be_awesome()
The shark is swimming. The shark is being awesome.
class Shark:
def swim(self):
print("The shark is swimming.")
def be_awesome(self):
print("The shark is being awesome.")
def main():
sammy = Shark()
sammy.swim()
sammy.be_awesome()
if __name__ == "__main__":
main()
The shark is swimming. The shark is being awesome.
class Shark:
def __init__(self):
print("This is the constructor method.")
class Shark:
def __init__(self, name):
self.name = name
class Shark:
def __init__(self, name):
self.name = name
def swim(self):
# Reference the name
print(self.name + " is swimming.")
def be_awesome(self):
# Reference the name
print(self.name + " is being awesome.")
class Shark:
def __init__(self, name):
self.name = name
def swim(self):
print(self.name + " is swimming.")
def be_awesome(self):
print(self.name + " is being awesome.")
def main():
# Set name of Shark object
sammy = Shark("Sammy")
sammy.swim()
sammy.be_awesome()
if __name__ == "__main__":
main()
Sammy is swimming. Sammy is being awesome.
class Shark:
def __init__(self, name, age):
self.name = name
self.age = age
sammy = Shark("Sammy", 5)
class Shark:
def __init__(self, name):
self.name = name
def swim(self):
print(self.name + " is swimming.")
def be_awesome(self):
print(self.name + " is being awesome.")
def main():
sammy = Shark("Sammy")
sammy.be_awesome()
stevie = Shark("Stevie")
stevie.swim()
if __name__ == "__main__":
main()
Sammy is being awesome. Stevie is swimming.