• 技术文章 >常见问题 >Python常见问题

    Python如何查看对象有哪些内容和属性

    月亮邮递员月亮邮递员2020-03-27 13:57:13原创2661

    Python如何查看对象有哪些内容和属性

    1、查看python对象的内容方法:

    如下代码:

    a = [1,2,3]
    print(type(a))
    print(a)

    执行结果是:

    <class 'list'>
    [1, 2, 3]

    也就是说通过print(type(对象名))可以输出对象类型,print(对象名)可以输出对象的详细信息。

    2、查看一个对象有哪些属性

    使用dir命令,如

    text="string"
    dir(text)

    执行结果是:

    ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribut
    e__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt
    __', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', 
    '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'ca
    pitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', '
    isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', '
    replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip
    ', 'swapcase', 'title', 'translate', 'upper', 'zfill']

    更多技术请关注Python视频教程

    专题推荐:python 对象 查看
    上一篇:Python如何根据日期判断周几 下一篇:Python测试有什么用途

    相关文章推荐

    • Python交互式怎么写if语句• Python如何监控键盘按了什么键• Python如何根据日期判断周几

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网