简体   繁体   English

Python __str__和__add__输出

[英]Python __str__ and __add__ output

I have the following example code, however, the output from __add__ doesnt look the same as the __str__ output. 我有以下示例代码,但是__add__的输出看起来与__str__输出看起来不一样。 I need all output to look exactly like the __str__ output format. 我需要所有输出看起来与__str__输出格式完全一样。

class Foo:
    def __init__(self,x,y=0):
        self.x = x
        self.y = y
    def __str__(self):
        return str(str(self.x/self.y) + "%")
    def __add__(self,other):
        if self.y > 50:
            return str((self.x + other.x)/(self.y+other.y)) + "%"
        else:
            return str((self.x + other.x)/(self.y+other.y)) + "%"

__str__ output looks as such: __str__输出如下:

>>> a = Foo(1,3)
>>> print(a)
>>> 0.33333%

__add__ output: __add__输出:

>>> Foo(1,3) + Foo(0,0)
>>> '0.33333%'

Why are there single quotes when I used __add__ or __sub__ and not when I use __str__ ? 当我使用__add____sub__时,为什么会有单引号而不是当我使用__str__

As mentioned above: Your REPL shows quotes, when the result of the last operation is a string. 如上所述:当最后一个操作的结果是字符串时,您的REPL显示引号。 Your REPL doesn't show quotes, when you print a string (and it supresses the resulting None ). 当您打印字符串时,您的REPL不显示引号(并且它会压缩生成的None )。

As a minimal example of this behaviour, you can try this: 作为此行为的最小示例,您可以尝试这样做:

Python 3.2.4 (default, May 10 2013, 08:57:38) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 'Hello'
>>> a
'Hello'
>>> print(a)
Hello

Twice the same string, once displayed with, once without quotes. 两次相同的字符串,一次显示,一次没有引号。


Also consider returning an actual instance of Foo in your __add__ method: 还要考虑在__add__方法中返回Foo的实际实例:

class Foo:
    def __init__(self, x, y=0):
        self.x = x
        self.y = y
    def __str__(self):
        return str(self.x / self.y) + "%"
    def __add__(self, other):
        return Foo(self.x + other.x, self.y + other.y)

This way you can do eg Foo(1, 2) + Foo(3, 4) + Foo(5, 6) which you cannot with your code. 这样你可以做你不能用你的代码的Foo(1, 2) + Foo(3, 4) + Foo(5, 6)

This has nothing to do with your custom __add__ or even __str__ methods - this is a feature of the interactive interpreter. 这与您的自定义__add__甚至__str__方法__str__ - 这是交互式解释器的一个功能。

Usually, in a Python script, you'd need to use something like print to display output. 通常,在Python脚本中,您需要使用类似print来显示输出。

In the interactive however, interpreter there's a handy shortcut: You can enter any expression and it will evaluate it, and print the representation of its result. 然而,在交互式中,解释器有一个方便的快捷方式:您可以输入任何表达式 ,它将对其进行评估,并打印其结果的表示 This is often called a read–eval–print loop (REPL) . 这通常称为读取 - 评估 - 打印循环(REPL)

Notice the distinction above: It prints the representation of the result, not the result itself. 注意上面的区别:它打印结果的表示 ,而不是结果本身。 This is usually (for primitive types and common objects) another Python expression that would recreate the same object. 这通常是(对于基本类型和公共对象)另一个将重新创建同一对象的Python表达式。

So, in case of a string a = 'foo' the string is foo , but its representation is 'foo' : 因此,如果字符串a = 'foo'则字符串为foo ,但其表示形式为'foo'

>>> a = 'foo'
>>> print a
foo
>>> a
'foo'

To get the representation of an object, Python calls obj.__repr__ (there's also a shorthand repr(obj) which you can use). 为了获得对象的表示,Python调用obj.__repr__ (还有一个你可以使用的速记repr(obj) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM