【问题标题】:Is there a way to skip positional arguments for class instances?有没有办法跳过类实例的位置参数?
【发布时间】:2021-05-29 03:42:53
【问题描述】:

所以这个简单的程序应该只检查房产的现有所有者、当前所有者、销售价格和其他一些信息。我昨晚刚学了一点 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()

【问题讨论】:

  • 默认值必须在最后。考虑为这样的函数使用命名参数——在这里很难说出每个字段对应的内容。
  • 当需要这些参数时,应该使用位置参数。如果它们不是必需的,它们应该是具有默认值的关键字参数,然后您可以在函数中随意处理。
  • 要跳过一个参数,你必须默认它,例如param=None。为了默认一个参数,它后面不需要有任何 unDefaulted 参数。所以把它们移到最后。
  • 我可以举个例子作为答案吗?我对它应该是什么样子有点困惑。

标签: python python-3.x class arguments instance


【解决方案1】:

只是为了加强 cmets 中提到的一些要点并用整个代码进行说明 - 如果您想避免在构造对象时键入 None,最好的方法是简单地移动您的 currentOwner , previousOwnerforSale 参数在构造函数的末尾并将它们设置为 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。如果您向他们传递一个参数 - 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),尽管对于现实生活中的房屋销售,没有人会要求您支付额外的几美分。

【讨论】:

    猜你喜欢
    • 2021-06-17
    • 2012-08-21
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多