国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

基于Python __dict__與dir()的區(qū)別詳解

2020-02-16 10:32:06
字體:
供稿:網(wǎng)友

Python下一切皆對(duì)象,每個(gè)對(duì)象都有多個(gè)屬性(attribute),Python對(duì)屬性有一套統(tǒng)一的管理方案。

__dict__與dir()的區(qū)別:

dir()是一個(gè)函數(shù),返回的是list;

__dict__是一個(gè)字典,鍵為屬性名,值為屬性值;

dir()用來尋找一個(gè)對(duì)象的所有屬性,包括__dict__中的屬性,__dict__是dir()的子集;

并不是所有對(duì)象都擁有__dict__屬性。許多內(nèi)建類型就沒有__dict__屬性,如list,此時(shí)就需要用dir()來列出對(duì)象的所有屬性。

__dict__屬性

__dict__是用來存儲(chǔ)對(duì)象屬性的一個(gè)字典,其鍵為屬性名,值為屬性的值。

#!/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):passif __name__ == '__main__':#主程序  a = A()  print a.__dict__  #{'age': 2, 'name': 'xy'}  實(shí)例中的__dict__屬性  print A.__dict__    '''  類A的__dict__屬性  {  '__dict__': <attribute '__dict__' of 'A' objects>, #這里如果想深究的話查看參考鏈接5  '__module__': '__main__',        #所處模塊  'num': <property object>,        #特性對(duì)象   'class_f': <function class_f>,     #類方法  'static_f': <function static_f>,    #靜態(tài)方法  '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__ #與上述結(jié)果相同  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>,   等.....  '''

從上述代碼可知,

實(shí)例的__dict__僅存儲(chǔ)與該實(shí)例相關(guān)的實(shí)例屬性,

正是因?yàn)閷?shí)例的__dict__屬性,每個(gè)實(shí)例的實(shí)例屬性才會(huì)互不影響。

類的__dict__存儲(chǔ)所有實(shí)例共享的變量和函數(shù)(類屬性,方法等),類的__dict__并不包含其父類的屬性。

dir()函數(shù)

dir()是Python提供的一個(gè)API函數(shù),dir()函數(shù)會(huì)自動(dòng)尋找一個(gè)對(duì)象的所有屬性(包括從父類中繼承的屬性)。

一個(gè)實(shí)例的__dict__屬性僅僅是那個(gè)實(shí)例的實(shí)例屬性的集合,并不包含該實(shí)例的所有有效屬性。所以如果想獲取一個(gè)對(duì)象所有有效屬性,應(yīng)使用dir()。

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__']'''#因?yàn)槊總€(gè)類都有一個(gè)__doc__屬性,所以需要去重,去重后然后比較print set(dir(a)) == set(a_dict + A_dict + object_dict) #True            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 三穗县| 泌阳县| 天峨县| 温宿县| 梨树县| 穆棱市| 镇安县| 遵化市| 福鼎市| 兴宁市| 盈江县| 柞水县| 富源县| 贵港市| 庆云县| 克东县| 海门市| 杭锦后旗| 襄垣县| 张家界市| 仙居县| 锦屏县| 佛学| 亳州市| 开平市| 崇阳县| 盐池县| 赤峰市| 辉县市| 大埔县| 望江县| 海阳市| 封丘县| 龙游县| 临猗县| 百色市| 涪陵区| 临夏县| 馆陶县| 嘉黎县| 裕民县|