Python中使用重载构造函数初始化对象列表及报错解决
问题解决步骤
1. 修复列表添加错误(直接触发迭代异常)
你使用self.accounts.extend(Account)时,extend()要求传入可迭代对象(如列表、元组),但createAccount返回的是单个Account实例,这会导致accounts列表被错误替换为单个对象,后续迭代时抛出TypeError。
将extend改为append(用于添加单个元素):
self.accounts.append(Account)
2. 修复createAccount方法的参数错误
当前createAccount返回Account对象时,错误复用了当前实例的属性,导致所有新账户的属性完全相同。修正为使用传入的参数:
def createAccount(self, account_number_IBAN, account_holder_name, balance): return Account(account_number_IBAN, account_holder_name, balance)
3. 移除不必要的继承
TestAccount作为测试类,无需继承Account,继承会引入冗余的初始化逻辑。修改类定义并删除多余的super().__init__调用:
class TestAccount: # 移除(Account)继承 def __init__(self): self.account_ID_List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.account_Holder_Name = ["KASH", "ARRAIN", "ZAHEER", "AMJAD", "JOHN", "SURESH", "SAIIN", "CHAUDHARAIN", "SARDAR", "BLOSSOM"] self.account_balance = [0, 100, 23, 555, 10000, 0, 99, 342, 0, 0] self.accounts = [] for i in range(10): # 直接调用Account构造器创建对象,无需依赖createAccount new_account = Account(self.account_ID_List[i], self.account_Holder_Name[i], self.account_balance[i]) self.accounts.append(new_account)
4. 修复__str__方法的返回值错误
原方法返回元组,打印时会显示为零散元素,改为返回拼接后的字符串:
def __str__(self): return f"Account Holder Name: {self.__account_holder_name}\nAccount holder's ID: {self.__account_number_IBAN}\nAccount Balance: {self.__balance}"
5. 修复withdraw方法的逻辑顺序错误
原条件判断顺序混乱,导致提示信息错误,修正后:
def withdraw(self, amount): if amount <= 0: print("The amount must be greater than 0") elif amount > self.__balance: print("Insufficient balance to withdraw the requested amount") else: self.__balance -= amount
完整修正代码
Account.py
class Account: def __init__(self, account_number_IBAN, account_holder_name, balance): self.__account_holder_name = account_holder_name self.__account_number_IBAN = account_number_IBAN self.__balance = balance def get_AccountHolder_Name(self): return self.__account_holder_name def get_AccountHolder_IBAN(self): return self.__account_number_IBAN def get_Balance(self): return self.__balance def createAccount(self, account_number_IBAN, account_holder_name, balance): return Account(account_number_IBAN, account_holder_name, balance) def __str__(self): return f"Account Holder Name: {self.__account_holder_name}\nAccount holder's ID: {self.__account_number_IBAN}\nAccount Balance: {self.__balance}" def deposit(self, amount): if amount > 0: self.__balance += amount else: print("The amount must be greater than 0") def withdraw(self, amount): if amount <= 0: print("The amount must be greater than 0") elif amount > self.__balance: print("Insufficient balance to withdraw the requested amount") else: self.__balance -= amount
TestAccount.py
from Account import Account class TestAccount: def __init__(self): self.account_ID_List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.account_Holder_Name = ["KASH", "ARRAIN", "ZAHEER", "AMJAD", "JOHN", "SURESH", "SAIIN", "CHAUDHARAIN", "SARDAR", "BLOSSOM"] self.account_balance = [0, 100, 23, 555, 10000, 0, 99, 342, 0, 0] self.accounts = [] for i in range(10): new_account = Account(self.account_ID_List[i], self.account_Holder_Name[i], self.account_balance[i]) self.accounts.append(new_account) def printAllAccountsInfo(self): for index, account in enumerate(self.accounts): print(f"\nAccount {index + 1}:") print(account) obj = TestAccount() obj.printAllAccountsInfo()
运行修正后的代码,即可正常创建10个Account对象并存入列表,且能正确打印所有账户信息。
内容的提问来源于stack exchange,提问作者user2240741
相关产品推荐
相关产品推荐

