我正在阅读python:掌握设计艺术 在第3章示例中:有一个巨大的继承示例 这是关于房地产的应用 我用Pycharm编写了代码,但是出现了这个错误:
AttributeError: 'super' object has no attribute 'display'
错误出现在“出租”类别中: (我在代码中标记了它)
我搜索了StackOverflow并找到了其他一些问题的解决方案,但这对我没有帮助
class Property:
def __init__(self, square_feet='', beds='', baths='', **kwargs):
super().__init__(**kwargs)
self.square_feet = square_feet
self.num_bedrooms = beds
self.num_bathrooms = 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_bathrooms))
print()
def prompt_init():
return dict(square_feet=input("Enter the square feet: "),
beds=input("Enter number of bedrooms: "),
baths=input("Enter number of bathrooms: "))
prompt_init = staticmethod(prompt_init)
######
# without class function # !!!! validation function !!!!!!!
def get_valid_input(input_string, valid_option):
input_string += "({})".format(", ".join(valid_option))
response = input(input_string)
while response.lower() not in valid_option:
response = input(input_string)
return response
class House(Property):
valid_garage = ("attached", "detached", "none")
valid_fenced = ("yes", "no")
def __init__(self, num_stories='', garage='', fenced='', **kwargs):
super().__init__(**kwargs)
self.num_stories = num_stories
self.garage = garage
self.fenced = fenced
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))
def prompt_init():
parent_init = Property.prompt_init()
fenced = get_valid_input("Is the yard fenced?"
, House.valid_fenced)
garage = get_valid_input("Does the property have 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
prompt_init = staticmethod(prompt_init)
############### class with error
class Rental:
def __init__(self, rent='', furnished='', utilities='', **kwargs):
super().__init__(**kwargs) . ### (problem is here)
self.furnished = furnished
self.utilities = utilities
self.rent = rent
def display(self):
super().diplay()
print("RENTAL DETAILS")
print("rent: {}".format(self.rent))
print("estimated utilities: {}".format(self.utilities))
print("furnished: {}".format(self.furnished))
def prompt_init():
return dict(
rent=input("What is the monthly rent? "),
utilities=input("What are the estimated utilities? "),
furnished=get_valid_input("is property furnished? ",
("yes", "no"))
)
prompt_init = staticmethod(prompt_init)
class HouseRental(Rental, House):
def prompt_init():
init = House.prompt_init()
init.update(Rental.prompt_init())
return init
prompt_init = staticmethod(prompt_init)
init = HouseRental.prompt_init()
house = HouseRental(**init)
house.display()
答案 0 :(得分:0)
有一个错字。代替:
def display(self):
super().diplay()
输入:
def display(self):
super().display()