python特有方法有哪些

Python (166) 2023-04-30 09:37:22

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

1、__name__

获得类的名字。

>>>A.__name__
Out[9]:'A'

注意,实例是没有这个属性的。

2、__module__

获取模块名。ipython 并不知道它的模块名,因为结果为 main:

>>>A.__module__
Out[11]:'__main__'

3、__doc__

显示文档字符串。

>>>A.__doc__

4、__class__

python 一切皆对象,类也是对象,所有类都是 type 的对象。

>>>A.__class__
Out[13]:type

而实例的 class 则是它的类:

>>>A().__class__
Out[14]:__main__.A

因此我们可以获取实例的类名:

>>>A().__class__.__name__
Out[15]:'A'

以上就是python特有方法的整理,希望能对大家有所帮助。更多Python学习指路:python基础教程

THE END

发表回复