0

我正在使用PyContract(不是PyContracts)为类方法编写一些约束。作为后置条件,我想确保实例的内存地址没有改变,即id(self)在调用函数之前和之后应该是相同的。我怎样才能用 PyContract 做到这一点?我有以下(最小)代码:

class Individual:
    def append(self, chrom):
        """
            post:
                __old__.self is self
                len(__old__.self.chromosomes)+1 == len(self.chromosomes)
                self.chromosomes[-1] == chrom
        """
        self.chromosomes.append(chrom)

这里的约束问题是,在帖子中,我收到了这个错误:_holder instance has no attribute 'self'

这里有趣的是,class Individual__init__的约束看起来像这样:

pre:
    isinstance(chromosomes, list)
post[chromosomes]:
    __old__.chromosomes is chromosomes
    __old__.chromosomes == chromosomes
post:
    hasattr(self, 'chromosomes')
    self.chromosomes == chromosomes

据我所知,PyContract我不喜欢这样称呼__old__.self。我该如何解决这个问题?

4

1 回答 1

0

这似乎解决了它:

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)

资源

于 2012-10-23T00:56:32.067 回答