π Chapter 2
π‘Code
car_company = 'Ferrari'
car_detail_1 = [
{'color' : 'White'},
{'Hp' : '400'},
{'price' : '8000'}
]
car_company2 = 'BMW'
car_detail_2 = [
{'color' : 'Black'},
{'Hp' : '270'},
{'price' : '5000'}
]
car_company3 = 'Audi'
car_detail_3 = [
{'color' : 'Silver'},
{'Hp' : '300'},
{'price' : '6000'}
]
car_company_list = ['Ferrari', 'BMW', 'Audi']
car_detail_list = [
{'color' : 'White', 'Hp' : '400', 'price' : '8000'},
{'color' : 'Black', 'Hp' : '270', 'price' : '5000'},
{'color' : 'Silver', 'Hp' : '300', 'price' : '6000'}
]
del car_detail_list[1]
del car_company_list[1]
print(car_company_list)
print(car_detail_list)
print()
print()
car_dicts = [
{'car_company' : 'Ferrari', 'car_detail' : {'color' : 'White', 'Hp' : '400', 'price' : '8000'}},
{'car_company': 'Bmw', 'car_detail': {'color' : 'Black', 'horsepower': 270, 'price': 5000}},
{'car_company': 'Audi', 'car_detail': {'color' : 'Silver', 'horsepower': 300, 'price': 6000}}
]
del car_dicts[1]
print(car_dicts)
print()
print()
class Car():
def __init__(self, company, details):
self._company = company
self._details = details
def __str__(self):
return 'str : {} - {}'.format(self._company, self._details)
def __repr__(self):
return 'repr : {} - {}'.format(self._company, self._details)
car1 = Car('Ferrari', {'color' : 'White', 'horsepower': 400, 'price': 8000})
car2 = Car('Bmw', {'color' : 'Black', 'horsepower': 270, 'price': 5000})
car3 = Car('Audi', {'color' : 'Silver', 'horsepower': 300, 'price': 6000})
print(car1.__dict__)
print(car2.__dict__)
print(car3.__dict__)
car_list = []
car_list.append(car1)
car_list.append(car2)
car_list.append(car3)
print()
print(car_list)
print()
print()
for x in car_list:
print(repr(x))
class Car():
"""
Car class
Author : Kim
Date: 2021.04.03
"""
car_count = 0
def __init__(self, company, details):
self._company = company
self._details = details
Car.car_count += 1
def __str__(self):
return 'str : {} - {}'.format(self._company, self._details)
def __repr__(self):
return 'repr : {} - {}'.format(self._company, self._details)
def detail_info(self):
print('Current Id : {}'.format(id(self)))
print('Car Detail info : {} {}'.format(self._company, self._details.get('price')))
def __del__(self):
Car.car_count -= 1
car1 = Car('Ferrari', {'color' : 'White', 'horsepower': 400, 'price': 8000})
car2 = Car('Bmw', {'color' : 'Black', 'horsepower': 270, 'price': 5000})
car3 = Car('Audi', {'color' : 'Silver', 'horsepower': 300, 'price': 6000})
print(id(car1))
print(id(car2))
print(id(car3))
print(car1._company == car2._company)
print(car1 is car2)
print(dir(car1))
print(dir(car2))
print()
print()
print(car1.__dict__)
print(car2.__dict__)
print(Car.__doc__)
print()
car1.detail_info()
car2.detail_info()
Car.detail_info(car1)
Car.detail_info(car2)
print(car1.__class__, car2.__class__)
print(id(car1.__class__) == id(car3.__class__))
print()
print(car1._company, car2._company)
print(car2._company, car3._company)
print()
print()
print(car1.car_count)
print(car2.car_count)
print(Car.car_count)
print()
print()
print(Car.__dict__)
print(car1.__dict__)
print(car2.__dict__)
print(car3.__dict__)
del car2
print(car1.car_count)
print(car3.car_count)
class Car():
"""
Car class
Author : Kim
Date: 2021.04.03
Description : Class, Static, Instance Method
"""
price_per_raise = 1.0
def __init__(self, company, details):
self._company = company
self._details = details
def __str__(self):
return 'str : {} - {}'.format(self._company, self._details)
def __repr__(self):
return 'repr : {} - {}'.format(self._company, self._details)
def detail_info(self):
print('Current Id : {}'.format(id(self)))
print('Car Detail info : {} {}'.format(self._company, self._details.get('price')))
def get_price(self):
return 'Before Car Price -> company : {}, price : {}'.format(self._company,self._details.get('price'))
def get_price_culc(self):
return 'After Car Price -> company : {}, price : {}'.format(self._company,self._details.get('price')*Car.price_per_raise)
@classmethod
def raise_price(cls,per):
if per <=1:
print('Please Enter 1 or More')
return
cls.price_per_raise = per
return 'Succeed! Price Incresed.'
@staticmethod
def is_Bmw(inst):
if inst._company == 'Bmw':
return 'Ok! This car is {}'.format(inst._company)
return 'Sorry. This car is not BMW.'
car1 = Car('Ferrari', {'color' : 'White', 'horsepower': 400, 'price': 8000})
car2 = Car('Bmw', {'color' : 'Black', 'horsepower': 270, 'price': 5000})
print(car1)
print(car2)
print()
car1.detail_info()
car2.detail_info()
print()
print(car1.get_price())
print(car2.get_price())
print()
Car.price_per_raise = 1.2
print(car1.get_price_culc())
print(car2.get_price_culc())
Car.raise_price(1.6)
print()
print(car1.get_price_culc())
print(car2.get_price_culc())
print(car1.is_Bmw(car1))
print(car2.is_Bmw(car2))
print(Car.is_Bmw(car1))
print(Car.is_Bmw(car2))
print(Car.__doc__)