如何将__repr__与多个参数一起使用?

use*_*210 0 python python-3.x

(Python 3.5.2)

__repr__为我的一个课程定义如下:

class d():
    def __init__(self):
        self._values = []
        return
    def __pos__(self):
        return self._values[0]
    def __repr__(self, value):
        self._values.append(value)
        return
Run Code Online (Sandbox Code Playgroud)

现在我想要

x = d()
print(x,"a")
print(+x)
Run Code Online (Sandbox Code Playgroud)

回来

a

但我得到了

TypeError: __repr__() missing 1 required positional argument: 'value'

我尝试了一些变化,比如print(x),"a"没有运气.

che*_*ner 10

如果要控制如何显示类的实例,正确的方法是覆盖该__format__方法.通常,您可以覆盖的三种方法用于:

  1. __repr__ - 当对象需要在交互式解释器中显示时使用,通常作为调试辅助工具.它应该是一个字符串,可以在评估时重新创建对象.
  2. __str__- strprint尝试显示对象时传递或调用对象时使用.没有其他定义,它只是调用__repr__.这是实例的"默认"字符串表示形式.
  3. __format__- 当你的对象是一个参数时使用str.format.它接收:在替换字段中的可选项之后出现的格式规范(如果有)作为附加参数.

这是一个表示数字对的类的简单示例.用于分隔数字的字符可以通过格式规范进行配置.

class Pair():
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __format__(self, spec):
        return "{}{}{}".format(self.x, spec, self.y)

    def __str__(self):
        return "{:/}".format(self)

    def __repr__(self):
        return "Pair({}, {})".format(self.x, self.y) 
Run Code Online (Sandbox Code Playgroud)

它可以使用如下:

>>> x = Pair(1,2)
>>> x  # using __repr__
Pair(1, 2)
>>> str(x)   # using __str__, with a default separator of /
'1/2'
>>> print(x)  # uses __str__ implicitly
1/2
>>> "{}".format(x)  # no separator specified
'12'
>>> "{:-}".format(x)  # use - to separate the two numbers
'1-2'
Run Code Online (Sandbox Code Playgroud)


Qua*_*ris 5

有一些人说你不应该添加论点,__str__或者__repr__我不同意。

例如,如果要显示类对象的字符串表示形式,则可以编写一个custom __str__。但是,如果某些对象真的很长怎么办?有时您可能希望截断那些对象,有时您可能希望将它们完整。

class Compute:

    def __init__(self, foo, ...):
        self.foo = foo
        ...

    def __str__(self, trunc=False):
        return_str = ''
        for item in self.__dict__:
            if trunc:
                # If the length of the str representation of the item is long, shorten it.
                return_str += f'\n{item} = {self.__dict__[item] if len(str(self.__dict__[item] < 50) else str(self.__dict__[item])[:51]}'
            else:
                # This would be whatever your usual __str__ method returns
                return_str += f'\n{item} = {self.__dict__[item]}'

        return return_str
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令简单地调用它:

bar = Compute(foo=...)

print(bar.__str__(trunc=True))
Run Code Online (Sandbox Code Playgroud)

现在,如果您__init__包含任何长度特别长的对象(例如,您有大量的数据列表),它只会打印到第51个字符。

您还可以为希望如何打印截断的字符串添加更多逻辑。

我建议在dunder方法中将默认参数设置为False。这意味着,除非明确要求,否则该__str__方法可以正常运行。

当我想将类的对象同时打印到终端和日志文件时,就使用了此方法。我只希望将它们完整地打印到日志文件中,并在终端上打印一个截短的版本,所以我写了类似的东西。

希望这可以帮助。