【问题标题】:Why is my class method getting a 2 non-keyword positional keyword arguements TypeError?为什么我的类方法得到 2 个非关键字位置关键字争论 TypeError?
【发布时间】:2013-03-25 22:24:23
【问题描述】:

我是一个初学者程序员,正在编写一个程序来模拟银行,该银行跟踪各种银行账户(我不知道我在做什么,只是把它放在那里。)练习我新获得的对象面向编程知识。当我运行我的程序时,我的 Manager 类中创建帐户并将它们存储在列表中的部分出现错误。我将在下面发布整个源代码以及错误。随意更正您认为不正确的任何其他内容,我一直在寻找改进代码的方法。

# Virtual Bank
# 3/21/13

# Account Manager Class
class AccountManager(object):
    """Manages and handles accounts for user access"""
    # Initial
    def __init__(self):
        self.accounts = []

    # create account
    def create_account(self, ID, bal = 0):
        # Check for uniqueness? Possible method/exception??? <- Fix this
        account = Account(ID, bal)
        self.accounts.append(account)

    def get_account(self, ID):
        for account in self.accounts:
            if account.ID == ID:
                return account
            else:
                return "That is not a valid account. Sending you back to Menu()"
                Menu()

class Account(object):
    """An interactive bank account."""
    wallet = 0
    # Initial
    def __init__(self, ID, bal):
        print("A new account has been created!")
        self.id = ID
        self.bal = bal

    def __str__(self):
        return "|Account Info| \nAccount ID: " + self.id + "\nAccount balance: $" + self.bal


# Main        
AccManager = AccountManager
def Menu():
    print(
        """
0 - Leave the Virtual Bank
1 - Open a new account
2 - Get info on an account
3 - Withdraw money
4 - Deposit money
5 - Transfer money from one account to another
6 - Get exchange rates(Euro, Franc, Pounds, Yuan, Yen)
"""
        ) # Add more if necessary
    choice = input("What would you like to do?: ")
    while choice != "0":
        if choice == "1":
            id_choice = input("What would you like your account to be named?: ")
            bal_choice = float(input("How much money would you like to deposit?(USD): "))
            AccManager.create_account(ID = id_choice,bal = bal_choice)
            Menu()


Menu()

这是错误。在 AccManager 的 create_account 方法实例化一个新帐户后,我收到错误消息。

TypeError: create_account() takes at least 2 non-keyword positional arguments (0 given)

【问题讨论】:

    标签: python oop python-3.x


    【解决方案1】:

    您将create_account 定义为实例方法,但将其称为类方法。尝试将其更改为:

    @classmethod
    def create_account(cls, ID, bal=0):
        ...
    

    不过,您可能需要在某处保留您创建的帐户?

    【讨论】:

    • 我很确定他想将帐户保留在 AccountManager 对象中。
    • 什么是类方法?它没有在我的书中描述它,我似乎无法找到它的明确定义。但是,我是一个不知道什么是静态方法的白痴,所以如果你愿意,请忽略我。 :)
    【解决方案2】:

    AccManager.create_account 是一个unbound 方法,即它不属于任何对象。因此,当您调用它时,它需要参数

    (self, ID, bal = 0)
    

    但是,您在行中调用它时没有 self 的值

    AccManager.create_account(ID = id_choice,bal = bal_choice)
    

    你要创建一个AccManager对象,然后得到itscreate_account方法:

    am = AccManager() # Create object
    while choice != "0":
            if choice == "1":
                id_choice = input("What would you like your account to be named?: ")
                bal_choice = float(input("How much money would you like to deposit?(USD): "))
                am.create_account(ID = id_choice,bal = bal_choice)
                Menu()
    

    【讨论】:

    • 实际上,我意识到我在声明 AccManager 时忘记了 ()。不过感谢您的帮助。
    猜你喜欢
    • 2019-09-15
    • 2017-06-26
    • 2015-08-24
    • 2019-12-21
    • 2021-03-25
    • 2019-09-03
    • 2020-10-11
    • 2016-09-17
    • 1970-01-01
    相关资源
    最近更新 更多