class Property:
def __init__(self, square_feet='', beds='',
baths='', **kwargs):
super().__init__(**kwargs)
self.square_feet = square_feet
self.num_bedrooms = beds
self.num_baths = baths
def display(self):
print("PROPERTY DETAILS")
print("================")
print("square footage: {}".format(self.square_feet))
print("bedrooms: {}".format(self.num_bedrooms))
print("bathrooms: {}".format(self.num_baths))
print()
@staticmethod
def prompt_init():
return dict(square_feet=input("Enter the square feet: "),
beds=input("Enter number of bedrooms: "),
baths=input("Enter number of baths:"))
class Apartment(Property):
valid_laundries = ("coin", "ensuite", "none")
valid_balconies = ("yes", "no", "solarium")
def __init__(self, balcony='', laundry='', **kwargs):
super().__init__(**kwargs)
self.balcony = balcony
self.laundry = laundry
def display(self):
super().display()
print("APARTMENT DETAILS")
print("laundry: %s" % self.laundry)
print("has balcony: %s" % self.balcony)
@staticmethod
def prompt_init():
parent_init = Property.prompt_init()
laundry = get_valid_input('What laundry facilities does '
'the property have? ', Apartment.valid_laundries)
while laundry.lower() not in \
Apartment.valid_laundries:
laundry = input("What laundry facilities does "
"the property have? ({})".format(
", ".join(Apartment.valid_laundries)))
balcony = ''
while balcony.lower() not in \
Apartment.valid_balconies:
balcony = input(
"Does the property have a balcony? "
"({})".format(
", ".join(Apartment.valid_balconies)))
parent_init.update({
"laundry": laundry,
"balcony": balcony
})
return parent_init
class House(Property):
valid_garage = ("attached", "detached", "none")
valid_fenced = ("yes", "no")
def __init__(self, num_stories='',
garage='', fenced='', **kwargs):
super().__init__(**kwargs)
self.garage = garage
self.fenced = fenced
self.num_stories = num_stories
def display(self):
super().display()
print("HOUSE DETAILS")
print("# of stories: {}".format(self.num_stories))
print("garage: {}".format(self.garage))
print("fenced yard: {}".format(self.fenced))
@staticmethod
def prompt_init():
parent_init = Property.prompt_init()
fenced = get_valid_input("Is the yard fenced? ",
House.valid_fenced)
garage = get_valid_input("Is there a garage? ",
House.valid_garage)
num_stories = input("How many stories? ")
parent_init.update({
"fenced": fenced,
"garage": garage,
"num_stories": num_stories
})
return parent_init
def get_valid_input(input_string, valid_options):
input_string += " ({}) ".format(", ".join(valid_options))
response = input(input_string)
while response.lower() not in valid_options:
response = input(input_string)
return response
class Purchase:
def __init__(self, price='', taxes='', **kwargs):
super().__init__(**kwargs)
self.price = price
self.taxes = taxes
def display(self):
super().display()
print("PURCHASE DETAILS")
print("selling price: {}".format(self.price))
print("estimated taxes: {}".format(self.taxes))
@staticmethod
def prompt_init():
return dict(
price=input("What is the selling price? "),
taxes=input("What are the estimated taxes? "))
class Rental:
def __init__(self, furnished='', utilities='',
rent='', **kwargs):
super().__init__(**kwargs)
self.furnished = furnished
self.rent = rent
self.utilities = utilities
def display(self):
super().display()
print("RENTAL DETAILS")
print("rent: {}".format(self.rent))
print("estimated utilities: {}".format(
self.utilities))
print("furnished: {}".format(self.furnished))
@staticmethod
def prompt_init():
return dict(
rent=input("What is the monthly rent? "),
utilities=input(
"What are the estimated utilities? "),
furnished = get_valid_input(
"Is the property furnished? ",
("yes", "no")))
class HouseRental(Rental, House):
@staticmethod
def prompt_init():
init = House.prompt_init()
init.update(Rental.prompt_init())
return init
init = HouseRental.prompt_init()
print(init)
house = HouseRental(**init) #I assume this holds the input from all the prompt_init, from class Property to class Rental.
house.display()
내 질문은 super 클래스가 부모 클래스가없는 클래스와 상호 작용하는 방법입니다. 마지막 줄에서 house.display()가 호출되고 HouseRental 클래스는 상위 클래스가없는 두 클래스를 상속합니다. house.display()가 호출되면 Rental.display()를 먼저 호출 한 다음 Rental.display()의 super() 호출이 오류를 발생시키지 않고 다음 상속 된 클래스 인 House.display()를 호출하면 House.display()의 super() 호출이 상위 클래스 인 Property를 가리 킵니다.baseclass에 대한 super() 이해
이런 식으로 super()가 작동하는 방법에 대한 요지는 있지만 기본 클래스의 super()가 오류를 발생시키는 대신 MRO의 다음 클래스를 가리키는 이유를 완전히 이해하지 못합니다 (당신이 관심이 있다면) "아니오 부모 클래스 가능"
여기 house.display의 출력은 다음과 같습니다 파이썬에서
{'square_feet': '1', 'beds': '2', 'baths': '3', 'fenced': 'no', 'garage': 'none', 'num_stories': '3', 'rent': '1', 'utilities': '4', 'furnished': 'no'}
PROPERTY DETAILS
================
square footage: 1
bedrooms: 2
bathrooms: 3
HOUSE DETAILS
# of stories: 3
garage: none
fenced yard: no
RENTAL DETAILS
rent: 1
estimated utilities: 4
furnished: no
아, 그 사실을 완전히 잊었습니다. 그래서 이것을 올바르게 이해한다면, "객체"를 호출합니다.이 객체는 MRO의 다음 "객체"를 가리 킵니다. (context :'class HouseRental (Rental, House)') –
질문의 도메인 범위를 줄이기 위해 더 구체적인 작은 예를 만들면 도움이 될 것입니다. –