类 问题 - 程序按预期运行,但 visual studio 代码突出显示语法错误

Issue with Classes - Program works as intended, but visual studio code is highlighting syntax error

我的程序按预期工作,当我执行从账户 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

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

我相信继承在这里导致了一些混乱。虽然 Louis 和 Roman 都初始化了 filepath,但帐户基础 class 从未初始化 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是一个Class变量而不是实例变量

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)