最大递归深度超过 - Python Setter和Getters

时间:2017-08-16 22:45:13

标签: python class methods setter getter

我应该为这个类实例使用setter和getter,但在提示用户输入后会出现以下错误。

追踪(最近一次通话):   文件“python”,in   在 init 中输入文件“python”   在名称中输入“python”   在名称中输入“python”   在名称中输入“python”   [上一行重复492次] RecursionError:超出最大递归深度

class Pet:

  def __init__(self, nameOfPet, typeOfPet, ageOfPet):
    self.Name = nameOfPet
    self.Types = typeOfPet
    self.Age = ageOfPet

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

  @Name.setter
  def Name(self, petName):
    self.Name = petName

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

  @Types.setter
  def Types(self, petType):
    self.Types = petType

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

  @Age.setter
  def Age(self, petAge):
    self.Age = petAge

  def printPetInfo(self):
    print("Hello. Your pet's name is {}, it is a {} type and it is {} years old. ".format(self.Name,self.Types,self.Age))

#Get input from user.
name = input("Enter the pet's name: ")
types = input("Enter the pet's type (Dog, Cat, Bird, etc.): ")
age = input("Enter the pet's age (in years): ")
while age.isalpha():
  print()
  print("Age cannot contain numbers. Try again.")
  age = input("Enter the pet's age (in years): ")

#Instantiate a new Pet Instance.
myPet = Pet(name,types,age)

myPet.printPetInfo()

print("\n \n \n \n \n \n")

0 个答案:

没有答案