#!/usr/bin/python
# -*- coding: utf-8 -*-
class A(object):
class_var = 1
def __init__(self):
self.name = 'xy'
self.age = 2
@property
def num(self):
return self.age + 10
def fun(self):pass
def static_f():pass
def class_f(cls):pass
if __name__ == '__main__':#主程序
a = A()
print a.__dict__ #{'age': 2, 'name': 'xy'} 实例中的__dict__属性
print A.__dict__
'''
类A的__dict__属性
{
'__dict__': <attribute '__dict__' of 'A' objects>, #这里如果想深究的话查看参考链接5
'__module__': '__main__', #所处模块
'num': <property object>, #特性对象
'class_f': <function class_f>, #类方法
'static_f': <function static_f>, #静态方法
'class_var': 1, 'fun': <function fun >, #类变量
'__weakref__': <attribute '__weakref__' of 'A' objects>,
'__doc__': None, #class说明字符串
'__init__': <function __init__ at 0x0000000003451AC8>}
'''
a.level1 = 3
a.fun = lambda :x
print a.__dict__ #{'level1': 3, 'age': 2, 'name': 'xy','fun': <function <lambda> at 0x>}
print A.__dict__ #与上述结果相同
A.level2 = 4
print a.__dict__ #{'level1': 3, 'age': 2, 'name': 'xy'}
print A.__dict__ #增加了level2属性
print object.__dict__
'''
{'__setattr__': <slot wrapper '__setattr__' of 'object' objects>,
'__reduce_ex__': <method '__reduce_ex__' of 'object' objects>,
'__new__': <built-in method __new__ of type object at>,
等.....
'''
print dir(A) ''' ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'class_f', 'class_var', 'fun', 'level1', 'level2', 'name', 'num', 'static_f'] ''' a_dict = a.__dict__.keys() A_dict = A.__dict__.keys() object_dict = object.__dict__.keys() print a_dict print A_dict print object_dict ''' ['fun', 'level1', 'age', 'name'] ['__module__', 'level2', 'num', 'static_f', '__dict__', '__weakref__', '__init__', 'class_f', 'class_var', 'fun', '__doc__'] ['__setattr__', '__reduce_ex__', '__new__', '__reduce__', '__str__', '__format__', '__getattribute__', '__class__', '__delattr__', '__subclasshook__', '__repr__', '__hash__', '__sizeof__', '__doc__', '__init__'] ''' #因为每个类都有一个__doc__属性,所以需要去重,去重后然后比较 print set(dir(a)) == set(a_dict + A_dict + object_dict) #True
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有