-
카테고리
-
세부 분야
프로그래밍 언어
-
해결 여부
미해결
안녕하세요! Quiz8 코드부분중에 궁금한것이 있습니다!!
20.07.30 03:04 작성 조회수 150
0
house = []
house1 = House("강남", "아파트", "매매", "10억", "2010년")
house2 = House("마포", "오피스텔", "전세", "5억", "2007년")
house3 = House("송파", "빌라", "월세", "500/50", "2000년")
house.append(house1)
house.append(house2)
house.append(house3)
print("총 {0} 대의 매물이 있습니다.".format(len(house)))
for home in house:
home.show_detail()
위를 보면 house라는 리스트를 선언후 classHouse에 House(???)
가 들어가서 house라는 리스트에 추가한 다음 출력해주기 위하여
for문을 사용하여 home in house: home안에 house 안에 House
안에 show_detail()이 있으므로 home.show_detail()으로 출력
으로 이해하였는데 리스트를 이용하여 반복문을 이용하여 출력하는
방법밖에 없는지 궁금합니다.
답변을 작성해보세요.
0
redbean
2020.11.16
사용하는 쪽에서 사용하지 않고 생성만 해서 쓰려면 아래와 같이 쓰면 됩니다.
class House:
houseList = []
# 초기화
def __init__(self , location , house_type , deal_type , price ,completion_year):
self.location = location
self.house_type = house_type
self.deal_type = deal_type
self.price = price
self.completion_year = completion_year
self.houseList.append(self)
# 메물 정보 표시
def show_detail(self):
print("총 {0}개의 매물이 있습니다.".format(len(House.houseList)))
for house in House.houseList:
print(house.location , house.house_type , house.deal_type ,\
house.price ,house.completion_year)
h = House("강남","아파트","매매","10억","2010년")
h = House("마포","오피스텔","전세","5억","2007년")
h = House("송파","빌라","월세","500/50","2007년")
h.show_detail()
0
답변 2