有没有办法跳过类实例的位置参数?

2024-10-01 19:14:37 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,这个简单的程序应该只检查现有业主的财产,目前的业主,销售价格和其他一些事情。昨晚我刚学了一点oop,我想知道是否有办法忽略或跳过某些位置参数,让它们默认为类中的变量

所以在下面我的“house2”实例中,这代表了一所房子,它目前刚刚建成,因此没有任何当前所有者或以前的所有者

不要为我没有的值输入None,(以前的所有者,当前所有者),我可以说,“跳过位置参数‘当前所有者’和‘以前所有者’,只使用类中的变量”。这样我就不用为每个不存在的值键入None了

因此,我的实例将如下所示:

house2 = houseStats('77 Book Worm St', 'Inner-City', 1, 1, '120000')

与此相比:

house2 = houseStats('77 Book Worm St', 'Inner-City', 1, None, None, 1, '120000')

下面是完整的代码块:

# A simple program to get information of a house.

class houseStats:
    # class variables to default to if there are no instance variables.
    current_owner = 0
    previous_owner = 0
    forsale = 0

    def __init__(self, address, area, houseAge, currentOwner, previousOwner, forSale, salePrice):
        self.address = address
        self.area = area
        self.house_age = houseAge
        self.current_owner = currentOwner
        self.previous_owner = previousOwner
        self.forsale = forSale
        self.saleprice = salePrice
        # Function to determine the house age

    def houseage(self):
        return f"This house is {self.house_age} years old"

    # Function to determine whether the house is for sale and who sold it.
    def sold(self):
        if self.forsale is None:
            print("House is currently not for sale..")
        else:
            print(f'House is currently for sale for ${int(self.saleprice)}')


house1 = houseStats('19 Galaxy Way', 'Suburbs', 5, 'Douglas Forword', None, 1, 10000)
house2 = houseStats('77 Book Worm St', 'Inner-City', 1, None, None, 1, '120000')

house1.sold()

Tags: toselfnonecityforishouseinner
1条回答
网友
1楼 · 发布于 2024-10-01 19:14:37

为了强调注释中提到的一些要点,并用整个代码进行说明-如果您想避免在构建对象时键入None,最好的方法是简单地移动您的currentOwnerpreviousOwner在构造函数末尾设置参数,并将其设置为None

def __init__(self, address, area, houseAge, currentOwner, previousOwner, forSale, salePrice):

def __init__(self, address, area, houseAge, salePrice, forSale=None, currentOwner=None, previousOwner=None):

这样,如果您不向这些参数传递任何参数,Python将假定默认情况下它们的值为None。如果您向他们传递一个参数-将被该参数替换

您可以通过将此行添加到代码中来测试这一点:

class houseStats:
    def __init__(self, address, area, houseAge, salePrice, forSale=None, currentOwner=None, previousOwner=None):
    *** implement construction here***

    def __repr__(self):
        return f"Current owner is {self.current_owner} and previous owner is {self.previous_owner}"


house1 = houseStats('19 Galaxy Way', 'Suburbs', 5, 10000, 1)
house2 = houseStats('15 Milky Way', 'Somewhere', 1, 50000, 1, 'Firstname Lastname')
print(house1)  # Current owner is None and previous owner is None
print(house2)  # Current owner is Firstname Lastname and previous owner is None

但是您必须小心,因为它们仍然被视为位置参数,并且根据您构造对象的方式被覆盖。如果在house2对象示例中切换1和“Firstname Lastname”的位置,则输出将完全改变

您可能会使用*args,但根据程序的逻辑,这可能会变得很棘手

整个代码:

class houseStats:
    def __init__(self, address, area, houseAge, salePrice, forSale=None, currentOwner=None, previousOwner=None):
        self.address = address
        self.area = area
        self.house_age = houseAge
        self.saleprice = salePrice
        self.forsale = forSale
        self.current_owner = currentOwner
        self.previous_owner = previousOwner

    # Function to determine the house age
    def houseage(self):
        return f"This house is {self.house_age} years old"

    # Function to determine whether the house is for sale and who sold it.
    def sold(self):
        if self.forsale is None:
            print("House is currently not for sale..")
        else:
            print(f'House is currently for sale for ${int(self.saleprice)}')

请注意,您可以在F字符串的最后将int(self.saleprice)更改为self.saleprice,它仍然可以工作。在浮点数价格的情况下,您将收到准确的价格(例如10000.99),尽管在现实生活中没有人向您索要额外的几美分

相关问题 更多 >

    热门问题