简体   繁体   English

Python解释器的输出?

[英]Output of Python interpreter?

When using the Python interpreter, just writing 使用Python解释器时,只需编写

3+4

causes it to print '7' 使它打印“ 7”

But when importing some class ('Something', which has its own str and add implemented) and do this: 但是,当导入某个类(“ Something”,具有自己的stradd实现)并执行以下操作时:

a=Something(var2)
b=Something(var2)
a+b

the interpreter just prints a message reporting the action (EDIT: by this, I meant the message class.Something object at 0xspam..., which is the output of repr when it's not actually implemented, when I thought I'd get something like the output of str ), it doesn't prints the str version of the new object that was just created 解释器仅打印一条报告该操作的消息(编辑:通过这个,我的意思是消息类。0xspam ...处的某对象...,当我实际上想得到类似的东西时,这是repr的输出str的输出),它不会打印刚刚创建的新对象的str版本

Why does this happen? 为什么会这样? How can it be changed? 如何更改?

EDIT: The problem had to do with confusion over what str and repr do (I didn't know about that repr existed when I asked the question), I thought it was enough for a class to have str implemented to both being able to obtain a string from an object, and being able to print it directly to the interpreter without having to write 'print'. 编辑:问题与strrepr的功能混乱有关(当我问这个问题时,我不知道那个repr存在),我认为对于一个类来说,实现str就足够了来自对象的字符串,并且能够直接将其打印到解释器,而无需编写“ print”。 But the latter is something that's actually done by implementing repr appropiately 但是后者实际上是通过适当实施repr来完成的

Here is a class with its own add implemented: 这是一个实现了自己的add的类:

class A(object):

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

    def __add__(self, other):
        return self.__class__(self.x + other.x)

    def __repr__(self):
        return "%s with x=%s" % (self.__class__, self.x)

Now, let's create some instances and add them: 现在,让我们创建一些实例并添加它们:

>>> a = A(3); b = A(2)
>>> a + b
<class 't.A'> with x=5

Yes, it prints. 是的,它会打印。

Without the custom __repr__ as defined above, what prints would be the usual default representation, such as: 如果没有上面定义的自定义__repr__ ,那么什么打印将是通常的默认表示形式,例如:

<t.A object at 0x7f7d7bc9f990>

Here are the rules the interpreter uses: 以下是解释器使用的规则:

For anything but an expression statement, nothing is printed. 除表达式语句外,什么都不会打印。

For an expression statement, the result is assigned to _ , then the equivalent of this code happens: 对于一个表达式语句,将结果分配给_ ,然后将发生以下等效代码:

if _ is not None:
    try:
        print(repr(_))
    except Exception as e:
        print(<some message with e and object.__repr__(_)>)

That's really all there is to it. 这就是全部。

So, if nothing at all is getting printed, the most likely possibility is that a+b returns None here. 因此,如果什么都没打印,则最有可能的可能性是a+b在这里返回None For example, that Something.__add__ that you forgot to show us could look like this: 例如,您忘记向我们显示的Something.__add__可能如下所示:

def __add__(self, other):
    tmp = copy.copy(self)
    tmp += other
    # oops, no return tmp

Or maybe a+b returns an object whose repr is the empty string, or all whitespace, and you didn't notice that it printed a blank line. 也许a+b返回的对象的repr是空字符串或全空格,并且您没有注意到它打印了空行。

If something is getting printed, but it's not what you expected, then probably the __repr__ isn't defined the way you expected. 如果正在打印某些内容 ,但不是您期望的,则可能不是按照您期望的方式定义了__repr__ Since you only mention defining __str__ , you may not realize that __repr__ is a separate function, and if you just inherit the default, it tends to look like <__main__.Spam at 0x115f018d0> . 由于只提到了定义__str__ ,因此您可能没有意识到__repr__是一个单独的函数,并且如果您仅继承默认值,则它看起来像<__main__.Spam at 0x115f018d0>

print a+b instead of just a+b , of course print a+b而不是a+b

As to why a+b doesn't cause str() to be called on objects, I don't know. 至于为什么a+b不会导致str()在对象上被调用,我不知道。

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

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