内置类属性:python中每个类都拥有内置的类属性
__name__
__doc__
__dict__
__module__
__bases__
class Cat():
"""说明文档 猫类"""
number = 0
def __init__(self,name='',color=''):
self.name = name
self.color = color
def fun(self):
print('%s在跑'%self.name)
@staticmethod
def shout():
print('喵')
@classmethod
def get_number(cls):
print('猫的数量:%d'%cls.number)
class Animal:
"""动物类"""
if __name__ == '__main__':
cat1 = Cat('花','white')
"""
1.类.__name__
获取类的名字(字符串)
"""
print(Cat.__name__)
"""
2.类.__doc__
获取类的说明文档
"""
print(Cat.__doc__)
print(dict.__doc__)
"""
3.类.__dict__ :获取类中所有的类属性和对应的值,以键值对的形式存到字典里面
对象.__dict__ :将对象的属性和对应的值,转换成字典的元素(常用,记住)
"""
print(Cat.__dict__)
print(cat1.__dict__)
"""
4.类.__module__:获取当前类所在模块的名字
"""
print (Cat.__module__)
"""
5.类.__bases__:获取当前类的父类
"""
print(Cat.__bases__)
运行结果如下:
Cat
说明文档 猫类
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
{'__module__': '__main__', '__doc__': '说明文档 猫类', 'number': 0, '__init__': <function Cat.__init__ at 0x00000000026C8D90>, 'fun': <function Cat.fun at 0x00000000026C8E18>, 'shout': <staticmethod object at 0x00000000026D0320>, 'get_number': <classmethod object at 0x00000000026D0390>, '__dict__': <attribute '__dict__' of 'Cat' objects>, '__weakref__': <attribute '__weakref__' of 'Cat' objects>}
{'name': '花', 'color': 'white'}
__main__
(<class 'object'>,)
网友评论