类的收尾(1)

__doc__

表示类的描述信息

>>> class Foo:

...  ''' 描述信息 '''

...  def __init__(self):

...   print('hello,world')

...

>>> Foo.__doc__

' 描述信息 '

__module__

表示当前操作的对象在哪个模块

alben@Python:~/alben/learn_python$ cat iron.py

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

from june_five.superhero import Marvel

stark = Marvel('Tony')

print(stark.__module__)

alben@Python:~/alben/learn_python$ ./iron.py

june_five.superhero

__class__

表示当前实例属于哪个类

alben@Python:~/alben/learn_python$ cat iron.py

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

from june_five.superhero import Marvel

stark = Marvel('Tony')

print(stark.__class__)

alben@Python:~/alben/learn_python$ ./iron.py

<class 'june_five.superhero.Marvel'>

__init__

构造方法,通过类创建对象时自动运行

__str__

如果一个类中定义了__str__,那么对实例进行print的时候,会显示__str__的return值

>>> class Foo:

...   def __str__(self):

...    return 'ni shi SB'

...

>>> obj = Foo()

>>> print(obj)

ni shi SB

原文地址:https://www.cnblogs.com/alben-cisco/p/6947825.html