首页 > 文章列表 > python如何获取对象信息

python如何获取对象信息

python对象
376 2022-08-07

1、获取对象类型,基本类型可以用type()来判断。

>>> type(123)
<class 'int'>
>>> type('str')
<class 'str'>
>>> type(None)
<type(None) 'NoneType'>

2、如果想获得一个对象的所有属性和方法,可以使用dir()函数返回包含字符串的list。

>>> dir('ABC')
['__add__', '__class__',..., '__subclasshook__', 'capitalize', 'casefold',..., 'zfill']

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。