【发布时间】:2019-12-04 22:45:40
【问题描述】:
class Ticket:
def __init__(self, price):
self.price = price
@property
def price(self):
return self._price
@price.setter
def price(self, new_price):
if new_price < 0:
raise ValueError('Try again')
self._price = new_price
在上面的类定义中,self._price在price的两个函数中都使用了,但是变量_price在使用之前是没有定义的。
这个类像
t = Ticket(4)
print(t.price)
t.price = 34
我想知道这个类是如何工作的?变量_price在哪里定义?
【问题讨论】:
标签: python class methods attributes