You are on page 1of 61



• class






class Vehicle(object):
...

def main():
v1 = Vehicle()

main()

6
_ _init_ _(self):


car1
make
class Vehicle(object):
Ford
def __init__(self, makeParam, modelParam):
model
self.make = makeParam
Fiesta
self.model = modelParam
mpg
self.mpg = 0 0

def main(): car2


make
v1 = Vehicle("Ford", "Fiesta")
Scion
v2 = Vehicle("Scion", "xB")
model
xB
mpg
0



self

9
class Vehicle(object):
def calcTripCost(self, miles):
... #perform some calculations
return #new variable

def main():
v1 = Vehicle()
= v1.calcTripCost(100)
class Vehicle(object): v1
make
def __init__(self, make, model): Ford
self.make = make model
self.model = model Fiesta

self.mpg = 0 mpg
0

def main():
v1 = Vehicle("Ford", "Fiesta")
class Vehicle(object): v1
make
def __init__(self, make, model): Ford
self.make = make model
self.model = model Fiesta

self.mpg = 0 mpg
0

def main():
v1 = Vehicle("Ford", "Fiesta")
print("The MPG is", v1.mpg)

The MPG is 0
class Vehicle(object): v1
make
def __init__(self, make, model): Ford
self.make = make model
self.model = model Fiesta

self.mpg = 0 mpg
-100

def main():
v1 = Vehicle("Ford", "Fiesta")
print("The MPG is", v1.mpg)
v1.mpg = -100



setAttribute(self, newAttribute)




class Vehicle(object): v1
def __init__(self, make, model): make
self.make = make
Ford
self.model = model
self.mpg = 0 model
Fiesta
def setMPG(self, newMPG): mpg
if newMPG >= 0:
0
self.mpg = newMPG
else:
print("Invalid MPG")

def main():
v1 = Vehicle("Ford", "Fiesta")
class Vehicle(object): v1
def __init__(self, make, model): make
self.make = make
Ford
self.model = model
self.mpg = 0 model
Fiesta
def setMPG(self, newMPG): mpg
if newMPG >= 0:
0
self.mpg = newMPG
else:
print("Invalid MPG")

def main():
v1 = Vehicle("Ford", "Fiesta")
class Vehicle(object): v1
def __init__(self, make, model): make
self.make = make
Ford
self.model = model
self.mpg = 0 model
Fiesta
def setMPG(self, newMPG): mpg
if newMPG >= 0:
0
self.mpg = newMPG
else:
print("Invalid MPG")

def main():
v1 = Vehicle("Ford", "Fiesta")
v1.setMPG(-18)

Invalid MPG
class Vehicle(object): v1
def __init__(self, make, model): make
self.make = make
Ford
self.model = model
self.mpg = 0 model
Fiesta
def setMPG(self, newMPG): mpg
if newMPG >= 0:
23
self.mpg = newMPG
else:
print("Invalid MPG")

def main():
v1 = Vehicle("Ford", "Fiesta")
v1.setMPG(-18)
v1.setMPG(23)
class Fruit(object): kiwi

def __init__(self, nameParam, nutrParam): name kiwi


self.name = nameParam
self.nutritionDict = nutrParam nutritionDict

vitamin c
potassium
84
260

def main():
kiwi = Fruit("kiwi", {"vitamin c":84, "potassium":280})
class Fruit(object): kiwi

def __init__(self, nameParam, nutrParam): name kiwi


self.name = nameParam
self.nutritionDict = nutrParam nutritionDict

vitamin c
potassium
84
260

def main():
kiwi = Fruit("kiwi", {"vitamin c":84, "potassium":280})
class Fruit(object): kiwi

def __init__(self, nameParam, nutrParam): name kiwi


self.name = nameParam
self.nutritionDict = nutrParam nutritionDict

vitamin c
potassium
84
260

def main():
kiwi = Fruit("kiwi", {"vitamin c":84, "potassium":280})
print(kiwi.name, ":", kiwi.nutritionDict["vitamin c"], "mg Vitamin C")

kiwi has 84 mg vitamin C


class Fruit(object): kiwi

def __init__(self, nameParam, nutrParam): name kiwi


self.name = nameParam
self.nutritionDict = nutrParam nutritionDict

vitamin c
potassium
84
260

def main():
kiwi = Fruit("kiwi", {"vitamin c":84, "potassium":280})
print(kiwi.name, ":", kiwi.nutritionDict["fiber"], "mg fiber")



getAttribute(self)


class Fruit(object): kiwi

... name kiwi


def getNutrition(self,key):
if key in self.nutritionDict: nutritionDict

return self.nutritionDict[key] vitamin c


potassium
else: 84
260
return 0

def main():
kiwi = Fruit("kiwi", {"vitamin c":84, "potassium":280})
print(kiwi.name, ":", kiwi.getNutrition("vitamin c"), "mg vitamin C")

kiwi has 84 mg vitamin C


class Fruit(object): kiwi

... name kiwi


def getNutrition(self,key):
if key in self.nutritionDict: nutritionDict

return self.nutritionDict[key] vitamin c


potassium
else: 84
260
return 0

def main():
kiwi = Fruit("kiwi", {"vitamin c":84, "potassium":280})
print(kiwi.name, ":", kiwi.getNutrition("fiber"), "mg fiber")

kiwi has 0 mg fiber


class Fruit(object): kiwi

... name kiwi


def getName(self):
return self.name nutritionDict

vitamin c
potassium
84
260

def main():
kiwi = Fruit("kiwi", {"vitamin c":84, "potassium":280})
print(kiwi.getName(), ":", kiwi.getNutrition("fiber"), "mg fiber")

kiwi has 0 mg fiber














http://www.visualcapitalist.com/millions-lines-of-code/




• Vehicle.py

class Vehicle(object):
def __init__(self):
• Program.py
– main()
– main()

– main() Vehicle
– Vehicle
Vehicle
• Program.py

from fileName import className

from Vehicle import Vehicle


class Vehicle(object):
def __init__(self, make, model):
self.make = make
self.model = model
self.mpg = 0

def main():
car1 = Vehicle("Ford", "Fiesta")
car2 = Vehicle("Scion", "xB")
car1
Vehicle make
make Ford

model
model Fiesta
mpg
car2
mpg 0
make
Scion
model
xB
mpg
0





47
class Vehicle(object):
__init__
numVehicles = 0
def __init__(self, make, model):
self.make = make
self.model = model
self.mpg = 0
Vehicle.numVehicles += 1

def main(): ClassName.variable


v1 = Vehicle("Ford", "Fiesta")
v2 = Vehicle("Scion", "xB")
car1
Vehicle make
make Ford

model
model Fiesta
mpg
car2
mpg 0
make
Scion
model
xB
Vehicle.numVehicles
mpg
0
def main():
print("Total num is", Vehicle.numVehicles)
v1 = Vehicle("Ford", "Fiesta")
print("Total num is", Vehicle.numVehicles)
v2 = Vehicle("Scion", "xB")
print("Total num is", Vehicle.numVehicles)

Total vehicles is 0
Total vehicles is 1
Total vehicles is 2





MAX_COURSES = 6

class Student(object):
def __init__(self, studentName, studentID):
self.name = studentName
self.idNumber = studentID
self.courses = []

def addCourse(self, course):


if len(self.courses) < MAX_COURSES:
self.courses.append(course)
class Student(object):
MAX_COURSES = 6

def __init__(self, studentName, studentID):


self.name = studentName
self.idNumber = studentID
self.courses = []

def addCourse(self, course):


if len(self.courses) < Student.MAX_COURSES:
self.courses.append(course)

54
def main():
msg = "hello world"



55
class Vehicle(object)
def __init__(self, make, model):
self.make = make



56
class Vehicle(object):
numVehicles = 0



57


Vehicle.showCount()

59
class Vehicle(object):
numVehicles = 0

def __init__(self, make, model):


self.make = make
self.model = model
Vehicle.numVehicles += 1

@staticmethod
def status():
print("Total number of Vehicles ", Vehicle.numVehicles)

def main():
v1 = Vehicle("Ford", "Fiesta")
Vehicle.status()






• __init__ __str__

• @staticmethod

You might also like