Python
Object Oriented Programming
Everything is an object
class - define a class
def - define a function or method
What is a class? A logical grouping of data and functions
What is logical grouping? Create a class where there is a logical connection between things rather than just throwing random thing together under a class.
Example: Customer, Product
class Customer(object):
def withdraw(self, amount):
Classes and Object
Classes - blueprints for creating object
When we define a class using class keyword, we haven't actually created an object.
class Customer(object):
def __init__(self, name, balance=0.0):
self.name = name
self.balance = balance
def withdraw(self, amount):
if amount > self.balance:
raise RuntimeError('Amount greater than available balance.')
self.balance -= amount
return self.balance
def deposit(self, amount):
self.balance += amount
return self.balance
- We call the __init__ method with argument to create an object
-
- jeff object, known as an instance, is an object of Customer class
- before Customer() is called, no customer object existed.
- There is always 1 Customer class regardless of how many instances created
jeff = Customer('Jeff', 1000.0)
what is self?
- Calling jeff.withdraw(100.0) puts those instructions to use on the jeff instance.
- when we define
- self is the instance of the Customer that withdraw is being called on.
def withdraw(self, amount):
Customer.withdraw(jeff, 100.0)
jeff.withdraw(100.0)
=
jeff = Customer('Jeff', 1000.0)
jeff.withdraw(100.0)
what is __init__?
- When we call __init__, we are in the process of creating an object.
- When we initialize object like:
class Customer(object):
def __init__(self, name, balance=0.0):
self.name = name
self.balance = balance
jeff = Customer('Jeff', 1000.0)
jeff = Customer(jeff, 'Jeff', 1000.0)
=
jeff.name = name
jeff.balance = balance
self.name = name
self.balance = balance
=
Be careful!
- Be careful what you __init__, make sure the object is fully-initialized.
class Customer(object):
def __init__(self, name):
self.name = name
def set_balance(self, balance=0.0):
self.balance = balance
def withdraw(self, amount):
if amount > self.balance:
raise RuntimeError('Amount greater than available balance.')
self.balance -= amount
return self.balance
def deposit(self, amount):
self.balance += amount
return self.balance
** don't introduce a new attribute outside of __init__ method
Instance Attribute and Method
- A methods have access to all the data contained on the instance of the object.
- They can access and modify anything previously set on self.
- Because they use self, they require an instance of the class in order to be used.
Static Methods
- Class attributes are attributes that are set at the class-level
- Normal attributes are introduced in __init__ method, but some attributes of a class hold for all instances in all cases.
class Car(object):
wheels = 4
def __init__(self, make, model):
self.make = make
self.model = model
mustang = Car('Ford', 'Mustang')
print mustang.wheels
# 4
print Car.wheels
# 4
Inheritance
- Inheritance is the process by which a "child" class derives the data and behavior of a "parent" class.
- Example:
We sell all types of vehicles, from motorcycles to trucks. The price of a vehicle is 5000 x number of wheels.
We buy trucks in 10000, cars in 8000 and motorcycles in 4000.
Inheritance
class Car(object):
def __init__(self, wheels, miles, make, model, year):
self.wheels = wheels
self.miles = miles
self.make = make
self.model = model
self.year = year
def sale_price(self):
return 5000.0 * self.wheels
def purchase_price(self):
return 8000 - (.10 * self.miles)
class Truck(object):
def __init__(self, wheels, miles, make, model, year):
self.wheels = wheels
self.miles = miles
self.make = make
self.model = model
self.year = year
def sale_price(self):
return 5000.0 * self.wheels
def purchase_price(self):
return 10000 - (.10 * self.miles)
Don't Repeat Yourself!
Inheritance
class Vehicle(object):
base_sale_price = 0
def __init__(self, wheels, miles, make, model, year):
self.wheels = wheels
self.miles = miles
self.make = make
self.model = model
self.year = year
def sale_price(self):
return 5000.0 * self.wheels
def purchase_price(self):
return self.base_sale_price - (.10 * self.miles)
class Car(Vehicle):
def __init__(self, wheels, miles, make, model, year):
self.wheels = wheels
self.miles = miles
self.make = make
self.model = model
self.year = year
self.base_sale_price = 8000
class Truck(Vehicle):
def __init__(self, wheels, miles, make, model, year):
self.wheels = wheels
self.miles = miles
self.make = make
self.model = model
self.year = year
self.base_sale_price = 10000
Inheritance
from abc import ABCMeta, abstractmethod
class Vehicle(object):
__metaclass__ = ABCMeta
base_sale_price = 0
wheels = 0
def __init__(self, miles, make, model, year):
self.miles = miles
self.make = make
self.model = model
self.year = year
def sale_price(self):
return 5000.0 * self.wheels
def purchase_price(self):
return self.base_sale_price - (.10 * self.miles)
@abstractmethod
def vehicle_type():
pass
class Car(Vehicle):
base_sale_price = 8000
wheels = 4
def vehicle_type(self):
return 'car'
class Truck(Vehicle):
base_sale_price = 10000
wheels = 4
def vehicle_type(self):
return 'truck'
class Motorcycle(Vehicle):
base_sale_price = 4000
wheels = 2
def vehicle_type(self):
return 'motorcycle'
Thank you
Python
By shirlin1028
Python
- 153