传递不同的输入参数时创建默认的类对象

时间:2012-07-02 19:07:54

标签: python oop class constructor

我有两个班级PairSequencePair对象有两个属性,position和value。 Sequence对象是一系列Pair个对象。

class Pair():
    def __init__(self, position, value):
        self.position = position
        self.value = value
        self.pair = (position, value)

    def __repr__(self):
        return "< Position: {0}, Value: {1} >".format(self.position, self.value)

class Sequence(Pair):
    def __init__(self, pairs):
        self.pairs = pairs
        self.cseg = [pair.value for pair in pairs]
        self.positions = [pair.position for pair in pairs]

    def __repr__(self):
        return "< Seq. {0} >".format(" ".join([str(x) for x in self.cseg]))

我可以用这个创建一个Sequence对象:

>>> Sequence([Pair(0, 2), Pair(2, 8), Pair(3, 1))
< Seq. 2 8 1 >
>>> Sequence.pairs
[< Position: 0, Value: 2 >,
 < Position: 2, Value: 8 >,
 < Position: 3, Value: 1 >]

如何创建仅提供Sequence值列表的Pair对象,如下面的代码?在这种情况下,Pair位置必须是从0到n - 1的序列,其中n是Sequence的长度。

>>> Sequence([2, 8, 1])
< Seq. 2 8 1 >
>>> Sequence.pairs
[< Position: 0, Value: 2 >,
 < Position: 1, Value: 8 >,
 < Position: 2, Value: 1 >]

我尝试使用此版本的Sequence,但它不起作用。我收到了这个错误:

AttributeError: 'int' object has no attribute 'value'

class Sequence(Pair):
    def __init__(self, pairs):

        if not isinstance(pairs[0], Pair):
            self = Sequence([Pair(pos, val) for pos, val in enumerate(pairs)])

        self.pairs = pairs
        self.cseg = [pair.value for pair in pairs]
        self.positions = [pair.position for pair in pairs]

    def __repr__(self):
        return "< Seq. {0} >".format(" ".join([str(x) for x in self.cseg]))

1 个答案:

答案 0 :(得分:4)

当您传入包含非Pair对象的列表时,以下内容将创建Pair个对象的列表,否则它只会将给定的Pair个对象列表分配给self.pairs

class Sequence(Pair):
    def __init__(self, pairs):
        if not isinstance(pairs[0], Pair):
            self.pairs = [Pair(pos, val) for pos, val in enumerate(pairs)]
        else:
            self.pairs = pairs
        self.cseg = [pair.value for pair in self.pairs]
        self.positions = [pair.position for pair in self.pairs]

您收到错误的原因是,尽管您检查了pairs的内容,但仍然将其分配给self.pairs

class Sequence(Pair):
    def __init__(self, pairs):
        # the following code will run when you're passing a list of integers
        if not isinstance(pairs[0], Pair):
            self = Sequence([Pair(pos, val) for pos, val in enumerate(pairs)])
        # but the next line will also run
        # which means self.pairs` is now a list of integers
        self.pairs = pairs
        # and that means pair in the following line is an int, not a Pair:
        self.cseg = [pair.value for pair in pairs]
        self.positions = [pair.position for pair in pairs]

最后,您不应在构造函数中执行以下操作(分配给self):

self = Sequence([Pair(pos, val) for pos, val in enumerate(pairs)])

您尝试“覆盖”或“替换”self对象,但这意味着您正在Sequence的构造函数中构建另一个Sequence对象。在这种情况下,这是不必要的,因为您可以在同一个构造函数中处理这两种情况。反过来,这会导致更清晰的代码。