print(car1) 과 print(car_list) 의 차이에 주의할 것, 객체를 직접출력할때와 객체를 리스트에 넣어서 출력할때 차이 있음.
print(car1) - str 로 출력
print(car_list) - 객체를 리스트에 넣어서 출력하면 __repr__ 로 출력
class Car():
def __init__(self, company, details):
self._company = company
self._details = details
def __str__(self): # 매직 메소드, 간단한 사용자 레벨의 출력
return 'str : {} - {}'.format(self._company, self._details)
# __str__ 함수를 구현하지 않으면 클래스의 주소가 출력된다.
# print(car1)
# <__main__.Car object at 0x7fd2081e1ac0>
# 구현하면 아래처럼 출력됨
# str : Ferrari - {'color': 'White', 'horsepower': 400, 'price': 8000}
def __repr__(self):
return 'repr : {} - {}'.format(self._company, self._details)
# print(car1) 출력에서 2개다 구현되어 있다면(__str__, __repr__ 모두 구현한 경우),
# __str__ 함수를 호출된다.
# __str__ 함수를 구현하지 않으면 __repr__ 함수가 자동 호출된다.
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)
print(car2)
print(car3)
// 객체속성까지 출력, 딕셔너리 형태로 출력됨
print(car1.__dict__)
print(car2.__dict__)
print(car3.__dict__)
-------------------------------------------[result]
str : Ferrari - {'color': 'White', 'horsepower': 400, 'price': 8000}
str : Bmw - {'color': 'Black', 'horsepower': 270, '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': 270, 'price': 5000}}
{'_company': 'Audi', '_details': {'color': 'Silver', 'horsepower': 300, 'price': 6000}}
-------------------------------------------
# dir 메서드를 이용하면, 모든 메타 정보가 출력됨, 리스트 형태로 출력됨
print(dir(car1))
# ['__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']
car_list = []
car_list.append(car1)
car_list.append(car2)
car_list.append(car3)
print(car_list)
-------------------------------------------[result]
[repr : Ferrari - {'color': 'White', 'horsepower': 400, 'price': 8000}, repr : Bmw - {'color': 'Black', 'horsepower': 270, 'price': 5000}, repr : Audi - {'color': 'Silver', 'horsepower': 300, 'price': 6000}]
-------------------------------------------
for x in car_list:
print(repr(x))
print(x)
-------------------------------------------[result]
repr : Ferrari - {'color': 'White', 'horsepower': 400, 'price': 8000}
str : Ferrari - {'color': 'White', 'horsepower': 400, 'price': 8000}
repr : Bmw - {'color': 'Black', 'horsepower': 270, 'price': 5000}
str : Bmw - {'color': 'Black', 'horsepower': 270, 'price': 5000}
repr : Audi - {'color': 'Silver', 'horsepower': 300, 'price': 6000}
str : Audi - {'color': 'Silver', 'horsepower': 300, 'price': 6000}
-------------------------------------------
'Python > Intermediate' 카테고리의 다른 글
[Python] NamedTuple - Magic Method(3) (0) | 2021.05.21 |
---|---|
[Python] Magic Method(2) (0) | 2021.05.21 |
[Python] Magic Method(1) (0) | 2021.05.21 |
[Python] Class(3) - Class Method, Static Method (0) | 2021.05.21 |
[Python] Class(2) - self, 매직 매소드 (0) | 2021.05.20 |