Professional Documents
Culture Documents
•
–
• 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
•
–
•
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
vitamin c
potassium
84
260
def main():
kiwi = Fruit("kiwi", {"vitamin c":84, "potassium":280})
class Fruit(object): kiwi
vitamin c
potassium
84
260
def main():
kiwi = Fruit("kiwi", {"vitamin c":84, "potassium":280})
class Fruit(object): kiwi
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")
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
def main():
kiwi = Fruit("kiwi", {"vitamin c":84, "potassium":280})
print(kiwi.name, ":", kiwi.getNutrition("vitamin c"), "mg vitamin C")
def main():
kiwi = Fruit("kiwi", {"vitamin c":84, "potassium":280})
print(kiwi.name, ":", kiwi.getNutrition("fiber"), "mg fiber")
vitamin c
potassium
84
260
def main():
kiwi = Fruit("kiwi", {"vitamin c":84, "potassium":280})
print(kiwi.getName(), ":", kiwi.getNutrition("fiber"), "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
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
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 = []
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
@staticmethod
def status():
print("Total number of Vehicles ", Vehicle.numVehicles)
def main():
v1 = Vehicle("Ford", "Fiesta")
Vehicle.status()
•
–
•
–
•
•
–
• __init__ __str__
–
• @staticmethod