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