我有一类,其中一个成员是整数序列。它可以是list
,tuple
或set
之一。在打印(__str__
类的实例时,我需要格式化整数,但要保留序列的类型。
例如,我有一个课程
class A:
def __init__(self, seq):
self.seq = seq
和三个实例
a = A(seq=tuple(range(5, 12)))
b = A(seq=list(range(17, 21)))
c = A(seq=set(range(26, 31)))
,我希望获得
的以下输出print(a) # A(seq=(0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b))
print(b) # A(seq=[0x11, 0x12, 0x13, 0x14])
print(c) # A(seq={0x1a, 0x1b, 0x1c, 0x1d, 0x1e})
我的第一次尝试是这样(它不起作用!这些项目以字符串表示。)
class A:
def __str__(self):
seq2 = self.seq.__class__(f"0x{i:02x}" for i in self.seq)
return f"A(seq={seq2})"
# A(seq=('0x05', '0x06', '0x07', '0x08', '0x09', '0x0a', '0x0b'))
# A(seq=['0x11', '0x12', '0x13', '0x14'])
# A(seq={'0x1b', '0x1c', '0x1d', '0x1a', '0x1e'})
一种可行但似乎很笨拙的方法是
class A:
def __str__(self):
item_str = ", ".join(f"0x{i:02x}" for i in self.seq)
if isinstance(self.seq, list):
o, c = "[", "]"
elif isinstance(self.seq, tuple):
o, c = "(", ")"
elif isinstance(self.seq, set):
o, c = "{", "}"
else:
raise ValueError
return f"A(seq={o}{item_str}{c})"
# A(seq=(0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b))
# A(seq=[0x11, 0x12, 0x13, 0x14])
# A(seq={0x1a, 0x1b, 0x1c, 0x1d, 0x1e})
没有更优雅的版本吗?还是我必须将list
,tuple
和set
的子类化为允许在__str__
中设置格式选项的版本?
答案 0 :(得分:2)
只需使用str.replace()
:
class A:
def __init__(self, seq):
self.seq = seq
def __str__(self):
seq2 = self.seq.__class__(f"0x{i:02x}" for i in self.seq)
return f"A(seq={seq2})".replace("'", '')
a = A(seq=tuple(range(5, 12)))
b = A(seq=list(range(17, 21)))
c = A(seq=set(range(26, 31)))
print(a)
print(b)
print(c)
打印:
A(seq=(0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b))
A(seq=[0x11, 0x12, 0x13, 0x14])
A(seq={0x1b, 0x1c, 0x1a, 0x1d, 0x1e})
答案 1 :(得分:2)
我会使用寻址词典:
class A:
def __str__(self):
wrappers = {
list : "[]",
tuple: "()",
set : "{}",
}
item_str = ", ".join(f"0x{i:02x}" for i in self.seq)
o, c = wrappers.get(type(self.seq), "||")
return f"A(seq={o}{item_str}{c})"