译(四十六)-Python输出对象信息

文章首发及后续更新:https://mwhls.top/3599.html
新的更新内容请到mwhls.top查看。
无图/无目录/格式错误/更多相关请到上方的文章首发页面查看。

stackoverflow热门问题目录

如有翻译问题欢迎评论指出,谢谢。

这篇的第三个回答我看不懂,所以我换成了第四个。

Python里有内置函数能输出一个对象的属性和值吗?
  • fuentesjr asked:

    • 我想知道 Python 有没有类似 PHP 的 print_r 函数。
    • 以便我查看对象的状态来调试脚本。
  • Answers:

    • Jeremy Cantrell – vote: 1159

    • 你需要的是 pprint()vars() 的组合:

    • from pprint import pprint
      pprint(vars(your_object))

  • user3850 – vote: 739

  • 你弄混了两个不同的东西。

  • dir()vars() 或者 inspect 模块来得到你想要的(我用 __builtins__来举例子,你可以用其它对象代替)。

  • >>> l = dir(__builtins__)
    >>> d = __builtins__.__dict__
  • 按你需要的方式输出:

  • >>> print l
    ['ArithmeticError', 'AssertionError', 'AttributeError',...
  • 或者

  • >>> from pprint import pprint
    >>> pprint(l)
    ['ArithmeticError',
    'AssertionError',
    'AttributeError',
    'BaseException',
    'DeprecationWarning',
    ...
    >>> pprint(d, indent=2)
    { 'ArithmeticError': ,
    'AssertionError': ,
    'AttributeError': ,
    ...
    '_': [ 'ArithmeticError',
           'AssertionError',
           'AttributeError',
           'BaseException',
           'DeprecationWarning',
    ...
  • Pretty printing 可作为命令用在交互式调试器中:

  • (Pdb) pp vars()
    {'__builtins__': {'ArithmeticError': ,
                    'AssertionError': ,
                    'AttributeError': ,
                    'BaseException': ,
                    'BufferError': ,
                    ...
                    'zip': },
    '__file__': 'pass.py',
    '__name__': '__main__'}
  • eduffy – vote: 83

  • 有人提到了 dir,但它只能输出属性名。如果你需要对应的值的话,可以试试 dict

  • class O:
     def __init__ (self):
        self.value = 3
    o = O()
  • 输出:

  • >>> o.__dict__
    {'value': 3}

  • Is there a built-in function to print all the current properties and values of an object?
    • fuentesjr asked:

      • So what I\’m looking for here is something like PHP\’s print_r function.
        我想知道 Python 有没有类似 PHP 的 print_r 函数。
      • This is so I can debug my scripts by seeing what\’s the state of the object in question.
        以便我查看对象的状态来调试脚本。
    • Answers:

      • Jeremy Cantrell – vote: 1159

      • You want vars() mixed with pprint():
        你需要的是 pprint()vars() 的组合:

      • from pprint import pprint
        pprint(vars(your_object))
      • user3850 – vote: 739

      • You are really mixing together two different things.
        你弄混了两个不同的东西。

      • Use dir(), vars() or the inspect module to get what you are interested in (I use __builtins__ as an example; you can use any object instead).
        dir()vars() 或者 inspect 模块来得到你想要的(我用 __builtins__来举例子,你可以用其它对象代替)。

      • >>> l = dir(__builtins__)
        >>> d = __builtins__.__dict__
      • Print that dictionary however fancy you like:
        按你需要的方式输出:

      • >>> print l
        ['ArithmeticError', 'AssertionError', 'AttributeError',...
      • or
        或者

      • >>> from pprint import pprint
        >>> pprint(l)
        ['ArithmeticError',
        'AssertionError',
        'AttributeError',
        'BaseException',
        'DeprecationWarning',
        ...
        >>> pprint(d, indent=2)
        { 'ArithmeticError': ,
        'AssertionError': ,
        'AttributeError': ,
        ...
        '_': [ 'ArithmeticError',
               'AssertionError',
               'AttributeError',
               'BaseException',
               'DeprecationWarning',
        ...
      • Pretty printing is also available in the interactive debugger as a command:
        Pretty printing 可作为命令用在交互式调试器中:

      • (Pdb) pp vars()
        {'__builtins__': {'ArithmeticError': ,
                        'AssertionError': ,
                        'AttributeError': ,
                        'BaseException': ,
                        'BufferError': ,
                        ...
                        'zip': },
        '__file__': 'pass.py',
        '__name__': '__main__'}
      • eduffy – vote: 83

      • dir has been mentioned, but that\’ll only give you the attributes\’ names. If you want their values as well try dict.
        有人提到了 dir,但它只能输出属性名。如果你需要对应的值的话,可以试试 dict

      • class O:
         def __init__ (self):
            self.value = 3
        o = O()
      • Here is the output:
        输出:

      • >>> o.__dict__
        {'value': 3}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值