gpt4 book ai didi

python - 我在从其他类继承时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 02:05:58 25 4
gpt4 key购买 nike

我刚刚创建了三个继承类,但出了点问题,但我尝试解决它但没有成功
我想知道是什么导致了这个错误以及我应该如何解决它
我应该继承其他类

  1. 类(class):
    • 属性(property)
    • 房子
    • 出租
    • 房屋出租

为什么House Object不带参数

在 Book python 3 object oriented Programming

这有点令人惊讶,因为它既没有 init 也没有显示方法!因为两个父类都在这些方法中适本地调用了 super,所以我们只需要扩展这些类,类就会以正确的顺序运行。 prompt_init当然不是这样,因为它是一个不调用super的静态方法,所以我们显式实现这个。在我们编写其他三个组合之前,我们应该测试这个类以确保它的行为正常:

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 Property:
def __init__(self, baths="", square_feet="",
beds="", **kwargs):
super().__init__(**kwargs)
self.num_baths = baths
self.square_feet = square_feet
self.num_beds = beds

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(sqare_feet=input("Enter The Square:"),
num_beds=input("Enter the Number of beds"),
num_baths=input("Enter the Number of baths"),)
class House(Property):
valid_garage = ("attached", "detached", "none")
valid_fenced = ("yes", "no")

def __init__(self, garage="", fenced="", num_stories="", **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))

@staticmethod
def prompt_init():
parent_init = Property.prompt_init()
garage = get_valid_input("Is the yard fenced? ", House.valid_garage)
fenced = get_valid_input("Is there a garage? ", House.valid_fenced)
num_stories = input("How many stories? ")

parent_init.update({
"garage": garage,
"fenced": fenced,
"num_stories": num_stories
})
return parent_init
class Rental:
def __init__(self, furnished="", rent="", utilities="", **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(House, Rental):

@staticmethod
def prompt_init():
init = House.prompt_init()
init.update(Rental.prompt_init())
return init

info = HouseRental().prompt_init()
o = HouseRental(**info)
o.display()
Traceback (most recent call last):
File "estate/placement.py", line 148, in <module>
o = HouseRental(**info)
File "estate/placement.py", line 68, in __init__
super().__init__(**kwargs)
File "estate/placement.py", line 13, in __init__
super().__init__(**kwargs)
File "estate/placement.py", line 117, in __init__
super().__init__(**kwargs)
TypeError: object.__init__() takes no parameters

最佳答案

在Rental类中,你没有指定父类,但是调用了super()。

Rental 应该是 Property 的一个子类吗?

如果是这样,只需将该类更改为:

class Rental(Property):
def __init__(self, furnished="", rent="", utilities="", **kwargs):
super().__init__(**kwargs)
self.furnished = furnished
self.rent = rent
self.utilities = utilities

类似地,Property 类调用 super() 但没有从父类继承。我不相信你打算让 Property 成为一个子类,所以删除 super() 调用:

class Property:
def __init__(self, baths="", square_feet="",
beds="", **kwargs):
self.num_baths = baths
self.square_feet = square_feet
self.num_beds = beds

更一般地说:NewClass(ParentClass) 形式导致 NewClass 从 ParentClass 继承方法和属性。 Parent 类的 init 函数采用的任何参数现在都可以安全地传递给 NewClass。调用 super().init(**kwargs) 获取传递给 NewClass 的任何关键字参数,并将它们传递给 ParentClass。

如果没有 ParentClass,则 NewClass 继承自 Python 基类 Object,它不带任何参数。将 (**kwargs) 传递给 Object 会引发错误。

回溯的最后一行描述了这一点:

object.__init__() takes no parameters

关于python - 我在从其他类继承时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54726778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com