Skip to content

Instantly share code, notes, and snippets.

@jet-c-21
Created May 31, 2022 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jet-c-21/0ddec8eb404ef236c754dbce02a3cff6 to your computer and use it in GitHub Desktop.
Save jet-c-21/0ddec8eb404ef236c754dbce02a3cff6 to your computer and use it in GitHub Desktop.
Python OOP Tutor - Drinks
# coding: utf-8
"""
author: Jet Chien
GitHub: https://github.com/jet-c-21
Create Date: 5/6/21
"""
class Drink:
company = 'jet drinks' # class var
drink_total = 0
def __init__(self, price, volume):
Drink.drink_total += 1
self.price = price # instance var
self.volume = volume
self.drink_id = Drink.drink_total
def __repr__(self):
return f"Drink <{self.drink_id}>"
def get_rate(self):
return self.price / self.volume
def print_company(self):
print(self.company)
@classmethod
def print_produced_count(cls):
print(cls.drink_total)
@classmethod
def change_company(cls, name):
# self.company = name
cls.company = name
def __del__(self):
Drink.drink_total -= 1
class Vodka(Drink):
def __init__(self, price, volume, alcohol_content):
super().__init__(price, volume)
self.alcohol_content = alcohol_content
def print_alcohol_content(self):
print(self.alcohol_content)
class Cola(Drink):
def __init__(self, price, volume, sugar):
super().__init__(price, volume)
self.sugar = sugar
cola = Cola(200, 500, 50)
print(cola)
# cola.change_company('mike company')
# cola.print_company()
vodka = Vodka(280, 300, 10)
print(vodka)
# vodka.print_company()
vodka.print_produced_count()
del vodka
cola.print_produced_count()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment