#!/usr/bin/env python
# coding: utf-8
#
# 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.
# Source: https://betterprogramming.pub/python-how-object-and-class-attributes-work-8edf4ed9caa4
# In[ ]:
class Person:
class_attr = "I am a class attribute"
def __init__(self, name=''):
self.name = name
# In[ ]:
person_1 = Person('Laura')
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
person_1.name
# In[ ]:
person_1.class_attr
# In[ ]:
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
# In[ ]:
person_1 = Person('Laura')
print(person_1.name)
# In[ ]:
# In[ ]:
# In[ ]:
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
# In[ ]:
# 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!)
# In[ ]:
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
# In[ ]:
p = Point(1, 4)
print(p)
# In[ ]:
p.x
# In[ ]:
p.y
# In[ ]:
p.addition()
# In[ ]:
q = Point(100, 2**3)
# In[ ]:
q.addition()
# In[ ]:
a = p.addition()*2 + q.addition()
# In[ ]:
print(a)
# In[ ]:
# In[ ]:
# # source: https://vegibit.com/python-class-examples/
# In[ ]:
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.')
# In[ ]:
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()
# In[ ]:
cool_new_vehicle = Vehicle('Honda', 'Ridgeline', 'Truck')
cool_new_vehicle.fuel_level = 7
# In[ ]:
# In[ ]:
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')
# In[ ]:
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.')
# In[ ]:
class ElectricVehicle(Vehicle):
def __init__(self, brand, model, type):
super().__init__(brand, model, type)
self.battery_size = 85
self.charge_level = 0
# In[ ]:
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!')
# In[ ]:
electric_vehicle = ElectricVehicle('Tesla', 'Model 3', 'Car')
electric_vehicle.charge()
electric_vehicle.drive()
# In[ ]:
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!')
# In[ ]:
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
# In[ ]:
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.')
# In[ ]:
electric_vehicle = ElectricVehicle('Tesla', 'CyberTruck', 'Truck')
electric_vehicle.charge()
print(electric_vehicle.battery.get_range())
electric_vehicle.drive()
# In[ ]:
class Vehicle:
"""Vehicle Class data and methods"""
class Battery:
"""Batter Class data and methods"""
class ElectricVehicle(Vehicle):
"""ElectricVehicle Class data and methods"""
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# # Source: https://www.digitalocean.com/community/tutorials/how-to-construct-classes-and-define-objects-in-python-3
# In[ ]:
class Shark:
def swim(self):
print("The shark is swimming.")
def be_awesome(self):
print("The shark is being awesome.")
# In[ ]:
sammy = Shark()
# In[ ]:
sammy = Shark()
sammy.swim()
sammy.be_awesome()
# In[ ]:
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()
# In[ ]:
class Shark:
def __init__(self):
print("This is the constructor method.")
# In[ ]:
class Shark:
def __init__(self, name):
self.name = name
# In[ ]:
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.")
# In[ ]:
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()
# In[ ]:
class Shark:
def __init__(self, name, age):
self.name = name
self.age = age
# In[ ]:
sammy = Shark("Sammy", 5)
# In[ ]:
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()
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]:
# In[ ]: