类问题-程序按预期工作,但visual studio代码高亮显示语法错误



我的程序正常工作,当我执行从帐户1到帐户2的转账时,它输出正确的值。

但是,visual studio代码中下划线为"self"红色(例如告诉我" 'Account'实例没有'filepath'成员)

你能帮我理解我做错了什么吗?非常感谢!

下面是我的代码和输出,非常感谢!
class Account:

def __init__(self):
filepath=self.filepath  #self is underlined here
with open(filepath,"r") as file:
self.balance = int(file.read())

def withdraw(self, amount):
self.balance -= amount

def deposit(self, amount):
self.balance += amount

def transfer(self, amount, receiver):
self.newbalance = self.balance - amount
receiver.newbalance = receiver.balance + amount
print(self.accountname,"balance is",self.balance) #self is underlined here
print(receiver.accountname,"balance is",receiver.balance)
print(self.accountname,"transfering",amount,"to",receiver.accountname) #self is underlined here
print(self.accountname, "new balance is",self.newbalance) #self is underlined here
print(receiver.accountname, "new balance is",receiver.newbalance) 

class Louis(Account):

accountname = "Louis" 
filepath = "louis.txt"

class Romain(Account):

accountname = "Romain"
filepath = "romain.txt"

romain = Romain()
louis = Louis()

romain.transfer(99,louis)

输出
Romain balance is 2000
Louis balance is 1000
Romain transfering 99 to Louis
Romain new balance is 1901
Louis new balance is 1099

我相信继承在这里导致了一些混乱。虽然Louis和Roman都初始化了filepath,但帐户基类从未初始化filepath。Visual studio将此标记为语法错误,因为如果您已经初始化了Account的实例,您的代码将引发AttributeError。例如:

class Account:    

def __init__(self):    
filepath=self.filepath  #self is underlined here    
with open(filepath,"r") as file:    
self.balance = int(file.read())    

def withdraw(self, amount):    
self.balance -= amount    

def deposit(self, amount):    
self.balance += amount    

def transfer(self, amount, receiver):    
self.newbalance = self.balance - amount    
receiver.newbalance = receiver.balance + amount    
print(self.accountname,"balance is",self.balance) #self is underlined here    
print(receiver.accountname,"balance is",receiver.balance)    
print(self.accountname,"transfering",amount,"to",receiver.accountname) #self is underlined here    
print(self.accountname, "new balance is",self.newbalance) #self is underlined here    
print(receiver.accountname, "new balance is",receiver.newbalance)     

class Louis(Account):    

accountname = "Louis"     
filepath = "louis.txt"    

class Romain(Account):    

accountname = "Romain"    
filepath = "romain.txt"    

romain = Romain()    
louis = Louis()    

romain.transfer(99,louis)    

account = Account() 

输出
Romain balance is 2000
Louis balance is 1000
Romain transfering 99 to Louis
Romain new balance is 1901
Louis new balance is 1099
Traceback (most recent call last):
File "/home/test.py", line 38, in <module>
account = Account()
File "/home/test.py", line 4, in __init__
filepath=self.filepath  #self is underlined here
AttributeError: 'Account' object has no attribute 'filepath'

问题是您只在子类中设置filepath。如果您尝试创建Account对象而不是LouisRomain,或者您使用另一个未设置filepath的子类,您将得到一个错误。VS Code没有任何方法知道你从来没有这样做过,它只是自己分析类。

你的filepath是一个变量不是实例变量

Louis和Romain需要成为Account的实例

class Account:    

def __init__(self, accountname, filepath):
self.filepath = filepath
self.accountname = accountname
self.balance = 0
with open(self.filepath,"r") as file:    
self.balance = int(file.read())    

def withdraw(self, amount):    
self.balance -= amount    

def deposit(self, amount):    
self.balance += amount    

def transfer(self, amount, receiver):    
self.newbalance = self.balance - amount    
receiver.newbalance = receiver.balance + amount    
print(self.accountname,"balance is",self.balance)
print(receiver.accountname,"balance is",receiver.balance)    
print(self.accountname,"transfering",amount,"to",receiver.accountname)
print(self.accountname, "new balance is",self.newbalance)
print(receiver.accountname, "new balance is",receiver.newbalance)     

romain = Account("Romain", "romain.txt")
louis = Account("Louis", "louis.txt")
romain.transfer(99,louis)

最新更新