首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“函数”对象没有属性'pprint‘

“函数”对象没有属性'pprint‘
EN

Stack Overflow用户
提问于 2013-09-07 07:04:17
回答 3查看 8.8K关注 0票数 1

在尝试使用pprint.pprint(object)打印对象时出现了此错误

我进口了from pprint import pprint..。

代码语言:javascript
复制
'function' object has no attribute 'pprint'

我的代码:

代码语言:javascript
复制
output = ', '.join([ pprint.pprint(p) for p in people_list])
    return HttpResponse (output)

我做错了什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-07 07:14:43

您已经导入了函数对象;关闭pprint.引用:

代码语言:javascript
复制
output = ', '.join([pprint(p) for p in people_list])
return HttpResponse (output)

这还不能满足您的要求,因为它将打印到sys.stdout not 将返回一个打印得很好的值。使用pprint模块并不非常适合在web服务器环境中使用。

我会创建一个PrettyPrinter实例并使用它的PrettyPrinter.pformat() method来生成输出:

代码语言:javascript
复制
from pprint import PrettyPrinter

pprinter = PrettyPrinter()
output = ', '.join([ pprinter.pformat(p) for p in people_list])

您也可以使用pprint.pformat(),但是只重复使用单个PrettyPrinter()对象更有效。

票数 4
EN

Stack Overflow用户

发布于 2013-09-07 07:06:46

from pprint import pprint只是从pprint模块导入pprint函数,然后尝试对该函数执行pprint.pprint

代码语言:javascript
复制
>>> from pprint import pprint
>>> pprint.pprint
Traceback (most recent call last):
    pprint.pprint
AttributeError: 'function' object has no attribute 'pprint'

只有pprint才能正常工作:

代码语言:javascript
复制
>>> pprint
<function pprint at 0xb6f40304>

要访问PrettyPrinterpprint模块的其他属性,只需导入pprint模块:

代码语言:javascript
复制
>>> import pprint
>>> pprint.pprint
<function pprint at 0xb6f40304>
>>> dir(pprint)
['PrettyPrinter', '_StringIO', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_commajoin', '_id', '_len', '_perfcheck', '_recursion', '_safe_repr', '_sorted', '_sys', '_type', 'isreadable', 'isrecursive', 'pformat', 'pprint', 'saferepr', 'warnings']
票数 4
EN

Stack Overflow用户

发布于 2018-08-08 05:35:04

如果您已经导入了pprint,那么您可以使用pprint("")

进口可以这样做.从pprint导入pprint

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18670725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档