Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在传递不同的输入参数时创建默认类对象_Python_Oop_Class_Constructor - Fatal编程技术网

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

Python 在传递不同的输入参数时创建默认类对象,python,oop,class,constructor,Python,Oop,Class,Constructor,我有两个类Pair和Sequence。对对象有两个属性,位置和值。序列对象是一个对对象序列 class Pair(): def __init__(self, position, value): self.position = position self.value = value self.pair = (position, value) def __repr__(self): return "< Posit

我有两个类
Pair
Sequence
对象有两个属性,位置和值。
序列
对象是一个
对象序列

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]))
如何创建一个只给出
值列表的
序列
对象,如下面的代码?在这种情况下,
位置必须是从0到n-1的序列,其中n是
序列
的长度

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

当您传入一个包含非
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]
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]
出现错误的原因是,尽管您检查了
对的内容
,但仍将其分配给
自我。对

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]
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
的构造函数中构造另一个
Sequence
对象。在这种情况下,这是不必要的,因为您可以在同一个构造函数中处理这两种情况。这样,反过来,会导致更干净的代码。

当您以非-
Pair
对象传入列表时,以下将创建
Pair
对象列表,否则它将只需将给定的
Pair列表分配给
self.Pair

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]
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]
出现错误的原因是,尽管您检查了
对的内容
,但仍将其分配给
自我。对

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]
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
的构造函数中构造另一个
Sequence
对象。在这种情况下,这是不必要的,因为您可以在同一个构造函数中处理这两种情况。这反过来又会导致更干净的代码