首页 > 编程语言 > 详细

Python的程序结构[2] -> 类/class -> 类的特殊属性

时间:2017-12-24 15:07:05      阅读:215      评论:0      收藏:0      [点我收藏+]

类的特殊属性 / Special Property of Class


Python 中通过 class 进行类的定义,类可以实例化成实例并利用实例对方法进行调用。

类中还包含的一些共有的特殊属性。

 

特殊类属性

含义

__name__

类的名字(字符串)

__doc__ 

类的文档字符串

__bases__

类的所有父类组成的元组

__dict__

类的属性组成的字典

__module__

类所属的模块

__class__

类对象的类型

 

 1 class Foo():
 2     """
 3     This is the text that can be called by __doc__
 4     """
 5     def __init__(self):
 6         self.foo = None
 7 
 8     def foo_method(self):
 9         self.foom = True
10 
11 print(>>>, Foo.__name__)
12 print(>>>, Foo.__doc__)
13 print(>>>, Foo.__bases__)
14 print(>>>, Foo.__dict__)
15 print(>>>, Foo.__module__)
16 print(>>>, Foo.__class__)

上面的代码定义了一个 Foo 类,并对类的基本特殊属性依次进行调用,最后得到结果如下,

>>> Foo
>>> 
    This is the text that can be called by __doc__
    
>>> (<class object>,)
>>> {__dict__: <attribute __dict__ of Foo objects>, __doc__: \n    This is the text that can be called by __doc__\n    , __weakref__: <attribute __weakref__ of Foo objects>, __init__: <function Foo.__init__ at 0x0301F930>, __module__: __main__, foo_method: <function Foo.foo_method at 0x0305C348>}
>>> __main__
>>> <class type>

 

Python的程序结构[2] -> 类/class -> 类的特殊属性

原文:http://www.cnblogs.com/stacklike/p/8098067.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!