您现在的位置是:课程教程文章

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

2023-12-18 19:24课程教程文章 人已围观

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测试有什么用途

下一篇:没有了

站点信息

  • 文章统计篇文章