首页 > python json 多态问题

python json 多态问题

import json

class Students(object):
def read(self):
print r'["Tim","Bob","Alice"]'
s = Students()

print json.load(s)
结果输出来但是出现警告Traceback (most recent call last)
但是如果把print换成return就好了 这是为什么呢


json.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])¶
Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object using this conversion table.

json.load方法是读一个file-like对象的read方法的返回值,所以要在read方法里返回一个json字符串,打印是不行的。

【热门文章】
【热门文章】