格式化序列中的整数(列表,元组,集合)

时间:2019-06-26 08:40:13

标签: python python-3.x python-3.6 python-3.7

我有一类,其中一个成员是整数序列。它可以是listtupleset之一。在打印(__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})

没有更优雅的版本吗?还是我必须将listtupleset的子类化为允许在__str__中设置格式选项的版本?

2 个答案:

答案 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})"