简体   繁体   English

涉及两个类时如何正确使用__repr__

[英]How to properly use __repr__ when two classes are involved

I am working on some Python code where I create a basic ATM. 我正在使用一些Python代码创建基本的ATM。 The issue I am having is I am not able to get the result that I want its printing "<'function Account.balance at 0x012CBC90>" Instead of the actual balance number. 我遇到的问题是我无法获得想要打印“ <'function Account.balance at 0x012CBC90>”的结果,而不是实际余额号的结果。 So far I have only tested using jsmith. 到目前为止,我仅使用jsmith进行了测试。 Feel free to call out any other issues that may cause a problem later. 随时提出可能会导致问题的其他任何问题。

class Account:

    def __init__(self,user,pin,balance):
        self.user = user
        self.pin = pin
        self.balance = int(balance)

    def get_user(self):
        return self.user

    def get_pin(self):
        return self.pin

    def balance(self):
        return int(self.balance)

    def setBalance(self,newBalance):
        self.balance = newBalance

    def __repr__(self):
        return str(self.user) + " " + str(self.pin) + " " + str(self.balance)


class ATM:

    def withdraw(self,Person,amount):
        result = Person - amount
        return result


    def check(self,Person):
        Person = Account.balance
        return str(Person)

    def transfer(self,id1,id2):
        pass

    def __str__(self):
        return self



    def main():

    Chase = ATM()

    Database = []

    Teron_Russell = Account("trussell",1738,0)
    Joe_Smith = Account("jsmith",1010,1350)

    print(Teron_Russell)

    Database.append(Teron_Russell)
    Database.append(Joe_Smith)

    print("Welcome to the ATM")
    id  = input("Please enter your user ID: ")
    pin = input("Enter your pin: ")
    chosen = ""

    for i in Database:
        print("Test1")
        name = Account.get_user(i)
        print(name)
        checkPin = Account.get_pin(i)
        print(checkPin)
        if id == name and pin == checkPin:
            chosen = i

    choice = input("What would you like to do. (Type 'Check','Withdraw','Transfer': ")

    if(choice == "Check" or "check"):
        print(Chase.check(chosen))



    # if(choice == "Withdraw" or "withdraw"):
    #     wAmount = eval(input("How much would you like to Withdraw: "))
    #  #   Chase.withdraw(Account.balance,)
    # elif(choice == "Check" or "check"):
    #             Chase.check()
    # else:
    #     print("Invalid Choice!")

if __name__ == "__main__":
    main()

You named a variable and a method the same name, so the interpreter is confused on which one to use. 您为变量和方法命名的名称相同,因此解释器会混淆使用哪个变量。 Change the name of either the method or variable balance and you won't have this problem. 更改方法或变量balance的名称,就不会出现此问题。 Additionally, this isn't java, and you shouldn't use classes for no reason. 另外,这不是Java,因此您不应无故使用类。 Since you aren't using any instance variables, it is pointless to have all of those methods inside that class. 由于您没有使用任何实例变量,因此将所有这些方法都包含在该类中是没有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM