코드 내용
# chapter02_01 #
# 객체 지향 프로그래밍(OOP) -> 코드의 재사용, 코드 중복 방지 등
# 규모가 큰 프로젝트(프로그램) -> 함수 중심 -> 데이터 방대 -> 복잡
# 클래스 중심 -> 데이터 중심 -> 객체로서 관리
#일반적인 코딩
car_company_1 = "Ferrari"
car_detail_1 = [
{'color' :'White'},
{'horsepower':400},
{'price':8000}
]
car_company_2 = "BMW"
car_detail_2 = [
{'color' :'Black'},
{'horsepower':200},
{'price':5000}
]
car_company_3 = "Audi"
car_detail_3 = [
{'color' :'Silver'},
{'horsepower':300},
{'price':6000}
]
#리스트 구조
#관리하기가 불편하다.
#index로 접근해야 하며, 하나의 자료가 사라지면 지워줘야한다.
car_company_list = ['Ferrari', 'BMW', 'Audi']
car_detail_list = [
{'color' :'White','horsepower':400, 'price':8000},
{'color' :'Black', 'horsepower':200, 'price':5000},
{'color' :'Silver', 'horsepower':300, 'price':6000}
]
#삭제해보기
del car_company_list[1]
del car_detail_list[1]
print(car_company_list, car_detail_list)
print()
print()
# 딕셔너리 구조
# 코드 반복 지속, 중첩 문제(키), 키 조회 예외 처리 < 관심
car_dicts = [
{'car_company': 'Farrari', 'car_detail':{'color' :'White','horsepower':400, 'price':8000}},
{'car_company': 'BMW', 'car_detail':{'color' :'Black', 'horsepower':200, 'price':5000}},
{'car_company': 'Audi', 'car_detail':{'color' :'Silver', 'horsepower':300, 'price':6000}}
]
#pop(key)
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)
def hi(self):
return "hello"
car1 = Car('Ferrari', {'color' :'White','horsepower':400, 'price':8000})
car2 = Car('BMW', {'color' :'Black','horsepower':500, 'price':5000})
car3 = Car('Audi', {'color' :'Silver','horsepower':300, 'price':6000})
print(car1)
print(car2)
print(car3)
print()
print()
# Object.__dict__ : 객체의 멤버변수들을 출력해주는 Magic method
print(car1.__dict__)
print(car2.__dict__)
print(car3.__dict__)
print()
print()
# dir(Object) : Object가 접근할 수 있는 Method 들을 list 형태로 반환
print(dir(car1))
print()
print()
car_list = []
car_list.append(car1)
car_list.append(car2)
car_list.append(car3)
print(car_list)
print()
print()
# Object.__str__ : 사용자 수준에서 객체의 정보를 str 형태로 반환
# Object.__repr__ : 개발자 수준에서 객체의 정보를 str 형태로 반환
# 반복
for item in car_list:
print(item)
print(repr(item))
print()
출력 결과
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
새로운 크로스 플랫폼 PowerShell 사용 https://aka.ms/pscore6
PS C:\Users\82107\Desktop\파이썬중급강의> conda activate base
PS C:\Users\82107\Desktop\파이썬중급강의> & C:/Users/82107/Anaconda3/python.exe c:/Users/82107/Desktop/파이썬중급강의/p_study/chapter02_01.py
['Ferrari', 'Audi'] [{'color': 'White', 'horsepower': 400, 'price': 8000}, {'color': 'Silver', 'horsepower': 300, 'price': 6000}]
[{'car_company': 'Farrari', 'car_detail': {'color': 'White', 'horsepower': 400, 'price': 8000}}, {'car_company': 'Audi', 'car_detail': {'color': 'Silver', 'horsepower': 300, 'price': 6000}}]
str : Ferrari - {'color': 'White', 'horsepower': 400, 'price': 8000}
str : BMW - {'color': 'Black', 'horsepower': 500, 'price': 5000}
str : Audi - {'color': 'Silver', 'horsepower': 300, 'price': 6000}
{'_company': 'Ferrari', '_details': {'color': 'White', 'horsepower': 400, 'price': 8000}}
{'_company': 'BMW', '_details': {'color': 'Black', 'horsepower': 500, 'price': 5000}}
{'_company': 'Audi', '_details': {'color': 'Silver', 'horsepower': 300, 'price': 6000}}
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_company', '_details', 'hi']
[repr : Ferrari - {'color': 'White', 'horsepower': 400, 'price': 8000} , repr
: BMW - {'color': 'Black', 'horsepower': 500, 'price': 5000} , repr : Audi - {'color': 'Silver', 'horsepower': 300, 'price': 6000} ]
str : Ferrari - {'color': 'White', 'horsepower': 400, 'price': 8000}
repr : Ferrari - {'color': 'White', 'horsepower': 400, 'price': 8000}
str : BMW - {'color': 'Black', 'horsepower': 500, 'price': 5000}
repr : BMW - {'color': 'Black', 'horsepower': 500, 'price': 5000}
str : Audi - {'color': 'Silver', 'horsepower': 300, 'price': 6000}
repr : Audi - {'color': 'Silver', 'horsepower': 300, 'price': 6000}