发布于 2016-07-08 15:59:14
不,你不应该把这个警告看作是详尽无遗的。以下代码在python2.x和python3.x上的行为不同,但没有发出警告:
from __future__ import print_function
class test(object):
def __nonzero__(self):
return False
t = test()
if t:
print('Hello')(至少不是OSX上的python2.7.10 )。好的,也许这段代码可以由2到3来处理(确实如此) --尽管我仍然期待着基于正式文件的警告。
通过向Python3中删除或显著更改的特性发出DeprecationWarning,警告Python3.x可能存在的不兼容。
下面是一些由2到3处理代码的示例,但是仍然没有使用-3选项发出警告:
from __future__ import print_function
import subprocess
class Pipe(object):
def __init__(self,openstr):
self.gnu_process=subprocess.Popen(openstr.split(),
stdin=subprocess.PIPE)
def put_in_pipe(self,mystr):
print(mystr, file=self.gnu_process.stdin)
if __name__=="__main__":
print("This simple program just echoes what you say (control-d to exit)")
p = Pipe("cat -")
while True:
try:
inpt = raw_input()
except EOFError:
break
print('putting in pipe:%s' % inpt)
p.put_in_pipe(inpt)这段代码的问题是,在python3中,我的管道需要一个字节对象,但是它得到了一个str (python2.x的unicode)。
https://stackoverflow.com/questions/38270882
复制相似问题