Python 3.x Python忽略缩进,并继续作为VSCode中类的一部分运行代码

Python 3.x Python忽略缩进,并继续作为VSCode中类的一部分运行代码,python-3.x,class,oop,recursion,indentation,Python 3.x,Class,Oop,Recursion,Indentation,好吧,这一个可能是完全愚蠢的,但现在开始。我使用VSCode在python中创建了一个简单的类。 然后我创建了一个类的实例,它一直运行并创建实例,直到 RecursionError:超过最大递归深度,好像未插入的代码是类的一部分 class Account: def __init__(self, initialCash): self.money = initialCash print("New account created. Initial budget: " +

好吧,这一个可能是完全愚蠢的,但现在开始。我使用VSCode在python中创建了一个简单的类。 然后我创建了一个类的实例,它一直运行并创建实例,直到 RecursionError:超过最大递归深度,好像未插入的代码是类的一部分

class Account:
   def __init__(self, initialCash):
      self.money = initialCash
      print("New account created. Initial budget: " + self.money)

   @property
   def money(self):
      return self.money

   @money.setter
   def money(self, value):
      self.money = value

account2 = Account(100)

缩进为3个空格,自动,如VSCode设置中所设置。我错过了什么?

自我。金钱调用setter,setter调用setter,setter

您必须重命名该属性:

class Account:
    def __init__(self, initial_cash):
        self._money = initial_cash
        print(f"New account created. Initial budget: {self.money}")

    @property
    def money(self):
        return self._money

    @money.setter
    def money(self, value):
        self._money = value

你应该使用4个空格。