Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created April 6, 2020 16:56
Show Gist options
  • Save azamsharp/d4d341d738d8db653d11bf04e770ae4d to your computer and use it in GitHub Desktop.
Save azamsharp/d4d341d738d8db653d11bf04e770ae4d to your computer and use it in GitHub Desktop.
Classes and Objects in Python
# Creating a Class
class Car:
def __init__(self, make, model): # initializer/constructor
# define properties of the car
self.make = make
self.model = model
self.color = "White"
self.vin = ""
def putFuel(self):
print("Put gasoline in car...")
def updateColor(self, color):
# check if the color is available
# then apply the color
self.color = color
def drive(self):
print("driving....")
def changeGear(self, gearNo):
print(f"changing gear to {gearNo}")
def brake(self):
print("brake..")
class ElectricCar(Car): # inheritence ElectricCar is inheriting from Car class
def __init__(self, make, model):
super().__init__(make, model) # super means parent class
self.battery = 100
self.range = 300
def displayRange(self):
print("display range")
#override the putFuel function
def putFuel(self):
print("Charge the car...")
tesla = ElectricCar("Tesla", "Model 3")
tesla.drive()
tesla.putFuel()
print(tesla.make)
print("Array....")
cars = []
car10 = Car("Honda", "Accord")
car20 = car10
car20.make = "Truck"
car10.make ??
#cars.append(car10)
#print(cars[0].make)
#cars[0].make = "Tesla"
#print(car10.make)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment