1、對(duì)象的屬性?
????python一切皆對(duì)象,每個(gè)對(duì)象都可能有多個(gè)屬性。python的屬性有一套統(tǒng)一的管理方案。?
屬性的__dict__系統(tǒng)?
????對(duì)象的屬性可能來自于其類定義,叫做類屬性;還可能是該對(duì)象實(shí)例自身定義的屬性,叫做對(duì)象屬性。類屬性可能來自類定義自身,也可能根據(jù)定義繼承而來。?
????對(duì)象的屬性存儲(chǔ)在對(duì)象的__dict__屬性中,__dict__是一個(gè)詞典,鍵為屬性名,值為屬性本身。例如:
class Bird(object):feather = Trueclass Chicken(Bird):fly = Falsedef __init__(self, age):self.age = agesummer = chicken(2)
print Bird.__dict__
print Chicken.__dict__
print summer.__dict__{'__dict__': <attribute '__dict__' of 'Bird' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'Bird' objects>, 'feather': True, '__doc__': None}{'fly': False, '__module__': '__main__', '__doc__': None, '__init__': <function __init__ at 0x2b91db476d70>}{'age': 2}
????可以看出,對(duì)類或?qū)ο?#xff08;實(shí)際類也是一類對(duì)象)調(diào)用__dict__方法,只是返回該類或?qū)ο笮略龅膶傩浴?strong>如果只有一個(gè)對(duì)象,而不知道它的類以及其他信息的時(shí)候,可以利用__class__屬性找到對(duì)象的類,然后調(diào)用類的__base__屬性來查詢父類。?
????可以通過__dict__來獲取和設(shè)置對(duì)象的屬性。
summer.__dict__['age'] = 3
print(summer.__dict__['age'])summer.age = 5
print(summer.age)
使用特殊方法__getattr__?
????可以使用 __getattr__(self, name) 來查詢即時(shí)生成的屬性,當(dāng)我們查詢一個(gè)屬性的時(shí)候,如果通過__dict__方法無法找到該屬性,那么python會(huì)調(diào)用該對(duì)象的__getattr__方法。
class bird(object):feather = Trueclass chicken(bird):fly = Falsedef __init__(self, age):self.age = agedef __getattr__(self, name):if name == 'adult':if self.age > 1.0: return Trueelse: return Falseelse: raise AttributeError(name)summer = chicken(2)
print(summer.adult)
summer.age = 0.5 #本身有age屬性
print(summer.adult) #本省沒有age屬性,會(huì)調(diào)用__getattr__方法
print(summer.male) #本省沒有age屬性,會(huì)調(diào)用__getattr__方法,拋出異常每個(gè)特性需要有自己的處理函數(shù),而__getattr__可以將所有的即時(shí)生成屬性放在同一個(gè)函數(shù)中處理。__getattr__可以根據(jù)函數(shù)名區(qū)別處理不同的屬性。
比如上面我們查詢屬性名male的時(shí)候,raise AttributeError。
?
python中還有一個(gè)__getattribute__特殊方法,用于查詢?nèi)我鈱傩?/strong>,而__getattr__只能用來查詢不在__dict__系統(tǒng)中的屬性。
2、閉包?
函數(shù)對(duì)象作用域?
????python中函數(shù)也是對(duì)象,函數(shù)對(duì)象也有其存活的范圍,就是函數(shù)對(duì)象的作用域。函數(shù)對(duì)象用def語句定義,函數(shù)對(duì)象的作用域與def所在的層級(jí)相同。比如,在函數(shù)A中定義內(nèi)部函數(shù)B,內(nèi)部函數(shù)B只能在定義該函數(shù)A內(nèi)部使用,不能在函數(shù)A外部使用。
def line_conf():def line(x):return 2*x+1print(line(5)) # within the scopeline_conf()
print(line(5)) # out of the scope
如果使用lambda定義函數(shù),那么函數(shù)對(duì)象的作用域與lambda所在的層級(jí)相同。
閉包?
????函數(shù)是一個(gè)對(duì)象,所以可以作為某個(gè)函數(shù)的返回結(jié)果。
def line_conf():b = 15def line(x):return 2*x+breturn line # return a function objectb = 5
my_line = line_conf()
print(my_line(5))
?
line定義的隸屬程序塊中引用了高層級(jí)的變量b,但b信息存在于line的定義之外,成b為line的環(huán)境變量。line作為line_conf的返回值時(shí),line中已經(jīng)包含了b的取值(盡管b并不隸屬于line).?
????一個(gè)函數(shù)和它的環(huán)境變量合在一起就構(gòu)成了一個(gè)閉包。在python中,閉包就是一個(gè)包含有環(huán)境變量取值的函數(shù)對(duì)象,環(huán)境變量取值被保存在函數(shù)對(duì)象的__closure__屬性中。比如:
def line_conf():b = 15def line(x):return 2*x+breturn line # return a function objectb = 5
my_line = line_conf()
print(my_line.__closure__)
print(my_line.__closure__[0].cell_contents)
?
__closure__里包含了一個(gè)元組,該元組中的每個(gè)元素都是cell類型的對(duì)象。
def line_conf(a, b):def line(x):return ax + breturn lineline1 = line_conf(1, 1)
line2 = line_conf(4, 5)
print(line1(5), line2(5))
只需要變換參數(shù)a,b,就可以獲得不同的直線表達(dá)函數(shù)。由此,我們可以看到,
閉包也具有提高代碼可復(fù)用性的作用。
?
3、裝飾器?
????裝飾器是一種高級(jí)的python語法,裝飾器可以對(duì)一個(gè)函數(shù)、方法或者類進(jìn)行加工。?
(1)裝飾函數(shù)和方法?
????裝飾器經(jīng)常用于對(duì)一些函數(shù)添加這些函數(shù)需要共同執(zhí)行的一些操作。
#定義裝飾器函數(shù),裝飾器名任意
def decorator(F):def new_F(a, b):print("input", a, b)return F(a, b)return new_F #返回一個(gè)可調(diào)用對(duì)象,該可調(diào)用對(duì)象以函數(shù)為參數(shù)# get square sum
@decorator #使用裝飾器進(jìn)行裝飾,在前面使用 @裝飾器名
def square_sum(a, b):return a**2 + b**2# get square diff
@decorator
def square_diff(a, b):return a**2 - b**2print(square_sum(3, 4))
print(square_diff(3, 4))
#使用裝飾器的效果等同于,將函數(shù)重定義
square_sum = decorator(square_sum)
square_sum(3, 4)
?
(2)含參的裝飾器?
????裝飾器允許我們?cè)谡{(diào)用裝飾器的時(shí)候提供其他參數(shù),比如 @decorator(params..)
#a new wrapper layer
def pre_str(pre=''):#old decoratordef decorator(F):def newF(a, b):print (pre + 'intput', a, b)return F(a,b)return newFreturn decorator@pre_str('xxxfjdflsd') #提供裝飾器參數(shù)
def square_num(a, b):return a**2 + b**2
?
(3)裝飾類?
????一個(gè)裝飾器可以接收一個(gè)類,并返回一個(gè)類,達(dá)到加工類的效果。
def decorator(aClass):class newClass(object):def __init__(self, age):self.total_display = 0self.wrapped = aClass(age)def display(self):self.total_display += 1print('total display', self.total_display)self.wrapped.display()return newClass@decorator
class Bird(object):def __init__(self, age):self.age = agedef display(self):print('my age is', self.age)eagleLord = Bird(5)for i in range(3):eagleLord.dislay()
?
參考:
http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html
總結(jié)
以上是生活随笔為你收集整理的python语法笔记(四)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。