我在测试的时候,发现下列代码的输出并不符合预期
def decorator(func):
def warp(self):
print('hello world dec')
return func(self)
return warp
class test():
def __init__(self):
print('hello world init')
@decorator
def __del__(self):
print('hello world del')
def __str__(self):
return 'hello world str'
print(test())
按我的设想,这个程序的输出顺序
应该是
hello world init
hello world str
hello world dec
hello world del
#最后的那句print全部执行完之后再销毁test
或者是
hello world init
hello world dec
hello world del
hello world str
#得到test的str之后就立即销毁
总之,因为单线程,所以应该是一句一句输出的。
但没想到的是,实际上的输出是下面这样:
hello world init
hello world str hello world dec
hello world del
难道print的流程是先销毁临时对象再输出一个换行符?
另外,str的那一句后面为什么会出现一个空格?
(我将输出重定向到文件再读取字符,确定是空格是32,换行是10,没有奇怪的ascii)