Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用functools.total_排序进行比较_Python_Python 3.x - Fatal编程技术网

Python 使用functools.total_排序进行比较

Python 使用functools.total_排序进行比较,python,python-3.x,Python,Python 3.x,我有下面的代码 class BankAccount: """ Simple BankAccount class """ def __init__(self, balance=0): """Initialize account with balance""" self.balance = balance def deposit(self, amount): """Deposit amount to this account

我有下面的代码

class BankAccount:
    """ Simple BankAccount class """

    def __init__(self, balance=0):
        """Initialize account with balance"""
        self.balance = balance

    def deposit(self, amount):
        """Deposit amount to this account"""
        self.balance += amount

    def withdraw(self, amount):
        """Withdraw amount from this account"""
        self.balance -= amount

    def __str__(self):
        return 'Account with a balance of {}'.format(self.balance)

    def __repr__(self):
        return "BankAccount(balance={})".format(self.balance)

    def __bool__(self):
        if self.balance > 0:
            return True
        else:
            return False
该代码基本上是一个简单的银行帐户模拟器。我想实现BankAccount对象的比较,这样就可以根据实例的余额进行比较。我想使用functools.total\u排序来完成此操作。预期输出低于

    account1 = BankAccount(100)
    account2 = BankAccount()
    account3 = BankAccount(100)
    account1 == account2
False
    account1 == account3
True
    account1 != account3
False
    account1 < account2
False
    account1 >= account2
True

如何执行此操作?

您只需定义至少一个函数:

__lt_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu__ 此外,该类还应提供一个_eq____)方法

然后使用装饰器,如下所示:

from functools import total_ordering

@total_ordering
class BankAccount:
""" Simple BankAccount class """

   def __init__(self, balance=0):
    """Initialize account with balance"""
       self.balance = balance
   def __lt__(self, other):
       return self.balance  < other.balance 
   def __eq__(self,other):
       return self.balance == other.balance

您只需定义至少一个函数:

__lt_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu__ 此外,该类还应提供一个_eq____)方法

然后使用装饰器,如下所示:

from functools import total_ordering

@total_ordering
class BankAccount:
""" Simple BankAccount class """

   def __init__(self, balance=0):
    """Initialize account with balance"""
       self.balance = balance
   def __lt__(self, other):
       return self.balance  < other.balance 
   def __eq__(self,other):
       return self.balance == other.balance

你看过报纸了吗?是否有任何不清楚的地方,您在具体的实现方面有问题吗?旁注:self.balance>0本身已经生成True或False;if…:return True,else:return False比需要的详细得多。只要用return self.balance>0来替换这一切,尽管return boolself.balance也可以。我对代码的def\u is\u valid\u操作数部分以及上面的代码是否需要它感到困惑。那里的文档链接到,它解释了何时返回NotImplemented。_is_valid_操作数方法实现了一种决定何时返回NotImplemented的形式。这是可选的,对于您的案例来说不是绝对必需的。如果您在问题正文中询问有关此混淆的问题,您的问题会得到更好的回答。您阅读了吗?是否有任何不清楚的地方,您在具体的实现方面有问题吗?旁注:self.balance>0本身已经生成True或False;if…:return True,else:return False比需要的详细得多。只要用return self.balance>0来替换这一切,尽管return boolself.balance也可以。我对代码的def\u is\u valid\u操作数部分以及上面的代码是否需要它感到困惑。那里的文档链接到,它解释了何时返回NotImplemented。_is_valid_操作数方法实现了一种决定何时返回NotImplemented的形式。这是可选的,对于你的案例来说不是绝对必需的。如果你在你的问题正文中询问关于这种混乱的问题,你的问题会被更好地接受。我有点困惑。那么我需要定义上面所有的函数吗?或者我可以只定义其中一个和eq,代码就可以自己解释其他函数了吗?因为我至少写了一个。所以一个或多个。在您的情况下,一个就足够了,如我的示例中所示:@total_ordering有名称错误的原因吗?您需要首先导入它。我编辑了我的例子,看看吧,我有点困惑。那么我需要定义上面所有的函数吗?或者我可以只定义其中一个和eq,代码就可以自己解释其他函数了吗?因为我至少写了一个。所以一个或多个。在您的情况下,一个就足够了,如我的示例中所示:@total_ordering有名称错误的原因吗?您需要首先导入它。我编辑了我的示例,请查看