这本书在讲解类时举了个例子
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
print(f"This car has {self.read_odometer()} miles on it. ")
my_new_car = Car('audi', 'a4', 2019)
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()
但是我在我自己电脑上运行此代码,编译器报错了:
Traceback (most recent call last):
File "D:\PycharmProjects\python_work\car.py", line 22, in <module>
my_new_car.read_odometer()
File "D:\PycharmProjects\python_work\car.py", line 18, in read_odometer
print(f"This car has {self.read_odometer()} miles on it. ")
File "D:\PycharmProjects\python_work\car.py", line 18, in read_odometer
print(f"This car has {self.read_odometer()} miles on it. ")
File "D:\PycharmProjects\python_work\car.py", line 18, in read_odometer
print(f"This car has {self.read_odometer()} miles on it. ")
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
我刚刚学Python,不是很懂为什么。