【发布时间】:2020-06-23 02:53:27
【问题描述】:
这是我的代码:
class BasicPlan(object):
def __init__(self, can_stream, can_download, has_SD, has_HD, has_UHD, num_of_devices, price):
self.can_stream = can_stream
self.can_download = can_download
self.has_SD = has_SD
self.has_HD = has_HD
self.has_UHD = has_UHD
self.num_of_devices = num_of_devices
self.price = price
def default(self):
self.can_stream = True
self.can_download = True
self.has_SD = True
self.has_HD = False
self.has_UHD = False
self.num_of_devices = 1
self.price = "$8.99"
在python shell中,我创建一个对象如下:
test = BasicPlan('randomValue', 'randomValue', 'randomValue', 'randomValue', 'randomValue', 'randomValue', 'randomValue')
然后我尝试应用默认方法,以便重新分配 self 值:
test = test.default()
现在我尝试打印一个属性:
print(test.can_stream)
我得到:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
print(test.can_stream)
AttributeError: 'NoneType' object has no attribute 'can_stream'
【问题讨论】:
-
test.default()返回None。分配后test变为None。删除test =。
标签: python class attributeerror self