首页 > 解决方案 > AttributeError:“超级”对象没有“显示”属性

问题描述

我正在阅读 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()

标签: pythonpython-3.x

解决方案


有一个错字。代替:

def display(self):
        super().diplay()

放:

def display(self):
        super().display()

推荐阅读