python代码2中的AttributeError
我有以下python代码行:python代码2中的AttributeError,python,Python,我有以下python代码行: class BankAccount: def __init__ (self,balance = 0): self.balance = balance def deposit(self, deposit_amount= 30): self.deposit_amount=deposit_amount balance += deposit_amount return balance def withd
class BankAccount:
def __init__ (self,balance = 0):
self.balance = balance
def deposit(self, deposit_amount= 30):
self.deposit_amount=deposit_amount
balance += deposit_amount
return balance
def withdraw (self,withdraw_amount= 10):
if withdraw_amount > balance:
raise RuntimeError('Invalid Transaction')
balance -= withdraw_amount
return balance
class MinimumBalanceAccount(BankAccount):
def __init__(self,balance = 0):
self.balance = balance
c = BankAccount()
c.deposit(50)
它给了我这个错误:
AttributeError("BankAccount instance has no attribute 'deposit'"
如果您的缩进实际上是您发布的缩进,则
存款
函数在\uuuu init\uuuu
方法中定义。因此,它不是类的属性,只是\uuuu init\uuu
方法中可用的函数。以下代码只是代码的副本,但缩进已修复:
class BankAccount:
def __init__(self,balance=0):
self.balance = balance
def deposit(self, deposit_amount=30):
self.deposit_amount=deposit_amount
self.balance += deposit_amount
return self.balance
def withdraw(self,withdraw_amount=10):
if withdraw_amount > self.balance:
raise RuntimeError('Invalid Transaction')
self.balance -= withdraw_amount
return self.balance
class MinimumBalanceAccount(BankAccount):
def __init__(self,balance = 0):
self.balance = balance
c = BankAccount()
c.deposit(50)
这段代码对我来说很有用,就像我用
c=MinimumBalanceAccount()
替换c=BankAccount()
时一样。如果您的缩进实际上是您发布的缩进,那么存款
函数是在\u init\u
方法中定义的。因此,它不是类的属性,只是\uuuu init\uuu
方法中可用的函数。以下代码只是代码的副本,但缩进已修复:
class BankAccount:
def __init__(self,balance=0):
self.balance = balance
def deposit(self, deposit_amount=30):
self.deposit_amount=deposit_amount
self.balance += deposit_amount
return self.balance
def withdraw(self,withdraw_amount=10):
if withdraw_amount > self.balance:
raise RuntimeError('Invalid Transaction')
self.balance -= withdraw_amount
return self.balance
class MinimumBalanceAccount(BankAccount):
def __init__(self,balance = 0):
self.balance = balance
c = BankAccount()
c.deposit(50)
这段代码对我很有用,就像我用
c=MinimumBalanceAccount()
替换c=BankAccount()
时一样。你有一些缩进和格式问题。你的缩进到处都是。请更正它以匹配实际代码好吗?不相关但很重要:在Python 2中,始终从对象
派生顶级类:类BankAccount(对象):…
。如果不这样做,您将无法看到对象和类的所有记录行为。您有一些缩进和格式问题。您的缩进到处都是。请更正它以匹配实际代码好吗?不相关但很重要:在Python 2中,始终从对象
派生顶级类:类BankAccount(对象):…
。如果不这样做,您将看不到对象和类的所有记录行为。请帮助更正BankAccount类MinimumBalanceAccount(BankAccount)第16行第16行中的此错误:NameError:名称“BankAccount”未定义只要BankAccount的类定义已完成,就应定义BankAccount
。我猜您正在尝试在BankAccount本身的定义中实例化(BankAccount()
),但是如果不看到代码,我就无法判断。还要记住,在您上面发布的代码中,MinimumBalanceAccount继承自BankAccount,但您过度编写了\uuuu init\uuu
方法。如果您希望它仍然像普通银行账户一样初始化,请调用BankAccount.\uuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu请帮助更正BankAccount类MinimumBalanceAccount(BankAccount)第16行中的此错误:名称错误:名称“BankAccount”未定义只要BankAccount的类定义已完成,就应定义BankAccount
。我猜您正在尝试在BankAccount本身的定义中实例化(BankAccount()
),但是如果不看到代码,我就无法判断。还要记住,在您上面发布的代码中,MinimumBalanceAccount继承自BankAccount,但您过度编写了\uuuu init\uuu
方法。如果您希望它仍然像普通银行帐户一样初始化,请调用BankAccount.\uuu init\uuuuuuuuuuuu(self,balance)
在MinimumBalanceAccount的\uuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu>中,代码就是我最初发布的代码(在页面顶部),请帮忙