【问题标题】:TypeError: unorderable types: atm() >= int()TypeError:不可排序的类型:atm() >= int()
【发布时间】:2015-04-25 13:22:00
【问题描述】:

我有 3 个类,ATM(主类)、atmFees(ATM 的子类)和交易。我想让我的类atmFees 继承父类ATM 的方法。

atmFees 类以atm 对象为参数,初始化为atm.__init__(self, balance)

我想重写父/超类的“提现”方法,修改其中一个参数——从amount 中减去 50 美分——然后用新的金额访问atm 中的超方法。

这样做会返回一个TypeError: unorderable types: atm() >= int()

我完全不知道从这里做什么,我几乎改变了所有东西,但我似乎无法让它发挥作用。

import transaction
import random

class atm(object):

    def __init__(self, bal):
        self.__balance = bal
        self.transactionList = []

    def deposit(self, name, amount):
        self.__balance += amount
        ndt = transaction.Transaction(name, amount)
        self.transactionList.append(ndt)

    def withdraw(self, name, amount):
        if self.__balance >= amount:
            self.__balance -= amount
            nwt = transaction.Transaction(name, amount)
            self.transactionList.append(nwt)
        else:
            print('Uh oh, not enough money!')

    def get_balance(self):
        return self.__balance

    def __str__(self):
        string_return = "" 
        for transaction in self.transactionList:
            string_return += str(transaction) + "\n"
        string_return = '\n' + 'The balance is $' + format(self.__balance, ',.2f')
        return string_return

class atmFee(atm):

    def __init__(self, balance):
        atm.__init__(self, balance)

    def widthrawal(cls, name, amount):
        amount = amount - .50
        atm.widthrawal(cls, name, amount)

    def deposit():
        pass

def main():

    myATM = atm.atm(75)
    fees  = atm.atmFee(myATM)

    fees.withdraw("2250",30)
    fees.withdraw("1000",20)
    myATM.deposit("3035",10)

    print("Let's withdraw $40")
    if myATM.withdraw("Amazon Prime",40) == 0:
        print ("Oh noes! No more money!")

    print()
    print("Audit Trail:")
    print(myATM)

main();

完整代码发布在这里: https://gist.github.com/markbratanov/e2bd662d7ff83ca5ef61

任何指导/帮助将不胜感激。

【问题讨论】:

  • 如果您发布一个最小的代码示例来重现问题中的问题,您可能会得到更好的答案。但看起来应该研究一下 Python 丰富的比较方法(尤其是__ge__)。
  • 添加了发生错误的代码。我会检查比较方法。

标签: python inheritance python-3.x overriding typeerror


【解决方案1】:

错误消息的意思就是它所说的 - 你不能订购一个对象和一个整数。这在 Python 2 中是可能的(出于某种原因),其中的顺序基本上是任意的(例如,一个空的 dict {} 总是大于一个整数,不管它有多大......),但它不在 Python 中3,因为比较是没有意义的。

【讨论】:

  • 我很困惑。 Python 2 文档,专门针对 CPython 说,不同类型的对象按类型名称排序 (docs.python.org/2/library/stdtypes.html),那么为什么 dict 对象总是 > int (这是正确的,我检查过)?跨度>
  • @cdarke:“除数字之外的不同类型的对象”。 int 是一个数字,不遵循该规则。
  • @Wooble:哇!谢谢。
【解决方案2】:

您可以这样创建 ATM 对象:

myATM = atm.atm(75)
fees  = atm.atmFee(myATM)

所以myATM 本身就是一个 ATM 对象,作为余额传递给atmFee.__init__。在withdraw 中,您希望余额是一个数字而不是 ATM 对象(如果比较有效,那么您对其执行的算术运算就会失败)。您几乎可以肯定是想通过创建这样的对象来将余额设置为一个数字:

fees = atm.atmFee(75)

请注意,atmFee 完全采用与超类相同的构造函数签名(这不是规则,但这是您在此处设置的方式),因此您应该在同样的方式。

您还在其余代码中使用 feesmyATM 之间切换,这看起来很奇怪。看起来您的意思是在所有情况下都使用fees,而实际上根本不需要myATM

【讨论】:

  • 工作完美,谢谢。问题,我想在初始化和取款时设置/更新父类ATM的balance,为什么没有发生?我已经调用了 super(引用 ATM.method()) ...
  • 例如,当我使用 atmFees 类(引用 ATM 方法提款())进行提款时,它似乎没有记录这些交易。
  • @markbratanov 粗略一看,我看不出有任何理由。您可能想发布一个新问题,确保包含显示交易未记录的代码。
猜你喜欢
  • 2016-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-05
  • 2016-12-18
相关资源
最近更新 更多