Best output type and encoding practices for __repr__() functions?

后端 未结 3 1445
你的背包
你的背包 2020-12-13 05:58

Lately, I\'ve had lots of trouble with __repr__(), format(), and encodings. Should the output of __repr__() be encoded or be

3条回答
  •  执笔经年
    2020-12-13 06:27

    I use a function like the following:

    def stdout_encode(u, default='UTF8'):
        if sys.stdout.encoding:
            return u.encode(sys.stdout.encoding)
        return u.encode(default)
    

    Then my __repr__ functions look like this:

    def __repr__(self):
        return stdout_encode(u''.format(self.abcd, self.efgh))
    

提交回复
热议问题