【发布时间】:2015-05-06 22:37:06
【问题描述】:
我有课
class ActivationResult(object):
def __init__(self, successful : bool):
self._successful = successful
def getSuccessful(self) -> bool:
return self._successful
还有一个测试
def testSuccessfulFromCreate(self):
target = ActivationResult(True)
self.assertEquals(target._successful, True)
self.assertEquals(target.getSuccessful, True)
第一个断言很好,但第二个断言失败,AssertionError: <bound method ActivationResult.getSuccess[84 chars]EB8>> != True
当我尝试打印它时,也会发生同样的事情。为什么?
【问题讨论】:
-
错误信息非常清楚 - 您在比较方法本身,而不是在调用时返回的值。此外,在 Python 中不需要 get/set;直接访问属性或使用
@property。 -
@PM2Ring:我尝试使用python.org/dev/peps/pep-3107 中提到的stackoverflow.com/a/21384492/254041(它不会产生任何错误或警告)。
-
@PM2Ring 那是一个函数注解
标签: python properties