Python 在PyContract约束中的` \旧``中引用'self'
我正在使用(not)为类方法编写一些约束。作为后置条件,我希望确保实例的内存地址没有更改,即调用函数前后Python 在PyContract约束中的` \旧``中引用'self',python,self,design-by-contract,contract,Python,Self,Design By Contract,Contract,我正在使用(not)为类方法编写一些约束。作为后置条件,我希望确保实例的内存地址没有更改,即调用函数前后id(self)应该相同。我怎样才能用PyContract做到这一点? 我有以下(最少)代码: 这里约束的问题是,在post中,我得到了这个错误:\u holder实例没有属性“self” 这里有趣的是,类个体有一个\uuuu init\uuuu,其约束如下所示: pre: isinstance(chromosomes, list) post[chromosomes]: __o
id(self)
应该相同。我怎样才能用PyContract做到这一点?
我有以下(最少)代码:
这里约束的问题是,在post中,我得到了这个错误:\u holder实例没有属性“self”
这里有趣的是,类个体
有一个\uuuu init\uuuu
,其约束如下所示:
pre:
isinstance(chromosomes, list)
post[chromosomes]:
__old__.chromosomes is chromosomes
__old__.chromosomes == chromosomes
post:
hasattr(self, 'chromosomes')
self.chromosomes == chromosomes
据我所知,PyContract
不喜欢我称之为\uuuu old\uuuu.self
。我该如何解决这个问题?这似乎可以解决这个问题:
class Individual:
def append(self, chrom):
"""
post[self]:
__old__.self is self
len(__old__.self.chromosomes)+1 == len(self.chromosomes)
self.chromosomes[-1] == chrom
"""
self.chromosomes.append(chrom)
class Individual:
def append(self, chrom):
"""
post[self]:
__old__.self is self
len(__old__.self.chromosomes)+1 == len(self.chromosomes)
self.chromosomes[-1] == chrom
"""
self.chromosomes.append(chrom)