当前位置: 首页 > 图灵资讯 > 行业资讯> python __dict__的使用注意

python __dict__的使用注意

来源:图灵python
时间: 2024-06-18 09:34:13

1、__dict__是属性,不是方法。

2、如果使用实例对象调用___dict__,输出由所有实例属性组成的字典。

3、使用类对象调用___dict__将输出由所有实例方法、类属性和类方法组成的字典。

实例

classPoloBlog:
sum=0

def__init__(self,name):
self.name=name

deftest(self):
pass

@classmethod
deftest_cls(cls):
pass

@staticmethod
deftest_static():
pass


blog=PoloBlog("小菠萝")
blog.test()
#调用实例对象
print(blog.__dict__)

#类对象调用
print(PoloBlog.__dict__)


#输出结果
{'name':'小菠萝'}
{'__module__':'__main__','sum':0,'__init__':<functionPoloBlog.__init__at0x105d2b0d>,'test':<functionPoloBlog.testat0x105d4d3>,'test_cls':<clasmethodobjectat0x105c47fa>,'test_static':<staticmethodobjectat0x105c47d9>,'__dict__':<attribute'__dict__'of'PoloBlog'objects>,'__weakref__':<attribute'__weakref__'of'PoloBlog'objects>,'__doc__':None}

以上是python __dict__注意使用,希望对大家有所帮助。更多Python学习指导:python基础教程

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