導航:首頁 > 編程語言 > 裝飾器模式python

裝飾器模式python

發布時間:2022-09-18 15:42:36

『壹』 請教python 使用裝飾器實現單例模式的原理

簡單來講,可以不嚴謹地把Python的裝飾器看做一個包裝函數的函數。 比如,有一個函數: def func(): print 'func() run.' if '__main__' == __name__: func() 運行後將輸出: func() run. 現在需要在函數運行前後列印一條日誌

『貳』 Python有哪些技術上的優點比其他語言好在哪兒

Python有哪些技術上的優點


1. 面向對象和函數式

從根本上講,Python是一種面向對象的語言。它的類模型支持多態、運算符重載和多重繼承等高級概念,並且以Python特有的簡潔的語法和類型為背景,OOP十分易於使用。事實上,即使你不懂這些術語,仍會發現學習Python比學習其他OOP語言要容易得多。

除了作為一種強大的代碼組織和重用手段以外,Python的OOP本質使它成為其他面向對象系統語言的理想腳本工具。例如,通過適當的粘接代碼,Python程序可以對C++、Java和C#的類進行子類的定製。

OOP只是Python的一個選擇而已,這一點非常重要。即使不能立馬成為一個面向對象高手,但你同樣可以繼續深入學習。就像C++一樣,Python既支持面向對象編程也支持面向過程編程的模式。如果條件允許,其面向對象的工具可以立即派上用場。這對策略開發模式十分有用,該模式常用於軟體開發的設計階段。

除了最初的過程式(語句為基礎)和面向對象(類為基礎)的編程範式,Python在最近幾年內置了對函數式編程的支持——一個多數情況下包括生成器、推導、閉包、映射、裝飾器、匿名lambda函數和第一類函數對象的集合。這是對其本身OOP工具的補充和替代。

2. 免費

Python的使用和分發是完全免費的。就像其他的開源軟體一樣,例如,Tcl、Perl、Linux和Apache。你可以從Internet上免費獲得Python的源代碼。你可以不受限制地復制Python,或將其嵌入你的系統或者隨產品一起發布。實際上,如果你願意的話,甚至可以銷售它的源代碼。

但請別誤會:「免費」並不代表「沒有支持」。恰恰相反,Python的在線社區對用戶需求的響應和商業軟體一樣快。而且,由於Python完全開放源代碼,提高了開發者的實力,並產生了一個很大的專家團隊。

盡管研究或改變一種程序語言的實現並不是對每一個人來說都那麼有趣,但是當你知道如果需要的話可以做到這些,該是多麼的令人欣慰。你不需要去依賴商業廠商的智慧,因為最終的文檔和終極的凈土(源碼)任憑你的使用。

Python的開發是由社區驅動的,是Internet大范圍的協同合作努力的結果。Python語言的改變必須遵循一套規范而有約束力的程序(稱作PEP流程),並需要經過規范的測試系統進行徹底檢查。正是這樣才使得Python相對於其他語言和系統可以保守地持續改進。

盡管Python 2.X和Python 3.X版本之間的分裂有力並蓄意地破壞了這項傳統,但通常它仍然體現在Python的這兩個系列內部。

『叄』 如何理解Python裝飾器

理解Python中的裝飾器
@makebold
@makeitalic
def say():
return "Hello"

列印出如下的輸出:
<b><i>Hello<i></b>

你會怎麼做?最後給出的答案是:

def makebold(fn):
def wrapped():
return "<b>" + fn() + "</b>"
return wrapped

def makeitalic(fn):
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped

@makebold
@makeitalic
def hello():
return "hello world"

print hello() ## 返回 <b><i>hello world</i></b>

現在我們來看看如何從一些最基礎的方式來理解Python的裝飾器。英文討論參考Here。
裝飾器是一個很著名的設計模式,經常被用於有切面需求的場景,較為經典的有插入日誌、性能測試、事務處理等。裝飾器是解決這類問題的絕佳設計,有了裝飾器,我們就可以抽離出大量函數中與函數功能本身無關的雷同代碼並繼續重用。概括的講,裝飾器的作用就是為已經存在的對象添加額外的功能。
1.1. 需求是怎麼來的?
裝飾器的定義很是抽象,我們來看一個小例子。

def foo():
print 'in foo()'
foo()

這是一個很無聊的函數沒錯。但是突然有一個更無聊的人,我們稱呼他為B君,說我想看看執行這個函數用了多長時間,好吧,那麼我們可以這樣做:

import time
def foo():
start = time.clock()
print 'in foo()'
end = time.clock()
print 'used:', end - start

foo()

很好,功能看起來無懈可擊。可是蛋疼的B君此刻突然不想看這個函數了,他對另一個叫foo2的函數產生了更濃厚的興趣。
怎麼辦呢?如果把以上新增加的代碼復制到foo2里,這就犯了大忌了~復制什麼的難道不是最討厭了么!而且,如果B君繼續看了其他的函數呢?
1.2. 以不變應萬變,是變也
還記得嗎,函數在Python中是一等公民,那麼我們可以考慮重新定義一個函數timeit,將foo的引用傳遞給他,然後在timeit中調用foo並進行計時,這樣,我們就達到了不改動foo定義的目的,而且,不論B君看了多少個函數,我們都不用去修改函數定義了!

import time

def foo():
print 'in foo()'

def timeit(func):
start = time.clock()
func()
end =time.clock()
print 'used:', end - start

timeit(foo)

看起來邏輯上並沒有問題,一切都很美好並且運作正常!……等等,我們似乎修改了調用部分的代碼。原本我們是這樣調用的:foo(),修改以後變成了:timeit(foo)。這樣的話,如果foo在N處都被調用了,你就不得不去修改這N處的代碼。或者更極端的,考慮其中某處調用的代碼無法修改這個情況,比如:這個函數是你交給別人使用的。
1.3. 最大限度地少改動!
既然如此,我們就來想想辦法不修改調用的代碼;如果不修改調用代碼,也就意味著調用foo()需要產生調用timeit(foo)的效果。我們可以想到將timeit賦值給foo,但是timeit似乎帶有一個參數……想辦法把參數統一吧!如果timeit(foo)不是直接產生調用效果,而是返回一個與foo參數列表一致的函數的話……就很好辦了,將timeit(foo)的返回值賦值給foo,然後,調用foo()的代碼完全不用修改!

#-*- coding: UTF-8 -*-
import time

def foo():
print 'in foo()'

# 定義一個計時器,傳入一個,並返回另一個附加了計時功能的方法
def timeit(func):

# 定義一個內嵌的包裝函數,給傳入的函數加上計時功能的包裝
def wrapper():
start = time.clock()
func()
end =time.clock()
print 'used:', end - start

# 將包裝後的函數返回
return wrapper

foo = timeit(foo)
foo()

這樣,一個簡易的計時器就做好了!我們只需要在定義foo以後調用foo之前,加上foo = timeit(foo),就可以達到計時的目的,這也就是裝飾器的概念,看起來像是foo被timeit裝飾了。在在這個例子中,函數進入和退出時需要計時,這被稱為一個橫切面(Aspect),這種編程方式被稱為面向切面的編程(Aspect-Oriented Programming)。與傳統編程習慣的從上往下執行方式相比較而言,像是在函數執行的流程中橫向地插入了一段邏輯。在特定的業務領域里,能減少大量重復代碼。面向切面編程還有相當多的術語,這里就不多做介紹,感興趣的話可以去找找相關的資料。
這個例子僅用於演示,並沒有考慮foo帶有參數和有返回值的情況,完善它的重任就交給你了 :)
上面這段代碼看起來似乎已經不能再精簡了,Python於是提供了一個語法糖來降低字元輸入量。

import time

def timeit(func):
def wrapper():
start = time.clock()
func()
end =time.clock()
print 'used:', end - start
return wrapper

@timeit
def foo():
print 'in foo()'

foo()

重點關注第11行的@timeit,在定義上加上這一行與另外寫foo = timeit(foo)完全等價,千萬不要以為@有另外的魔力。除了字元輸入少了一些,還有一個額外的好處:這樣看上去更有裝飾器的感覺。
-------------------
要理解python的裝飾器,我們首先必須明白在Python中函數也是被視為對象。這一點很重要。先看一個例子:

def shout(word="yes") :
return word.capitalize()+" !"

print shout()
# 輸出 : 'Yes !'

# 作為一個對象,你可以把函數賦給任何其他對象變數

scream = shout

# 注意我們沒有使用圓括弧,因為我們不是在調用函數
# 我們把函數shout賦給scream,也就是說你可以通過scream調用shout

print scream()
# 輸出 : 'Yes !'

# 還有,你可以刪除舊的名字shout,但是你仍然可以通過scream來訪問該函數

del shout
try :
print shout()
except NameError, e :
print e
#輸出 : "name 'shout' is not defined"

print scream()
# 輸出 : 'Yes !'

我們暫且把這個話題放旁邊,我們先看看python另外一個很有意思的屬性:可以在函數中定義函數:

def talk() :

# 你可以在talk中定義另外一個函數
def whisper(word="yes") :
return word.lower()+"...";

# ... 並且立馬使用它

print whisper()

# 你每次調用'talk',定義在talk裡面的whisper同樣也會被調用
talk()
# 輸出 :
# yes...

# 但是"whisper" 不會單獨存在:

try :
print whisper()
except NameError, e :
print e
#輸出 : "name 'whisper' is not defined"*

函數引用
從以上兩個例子我們可以得出,函數既然作為一個對象,因此:
1. 其可以被賦給其他變數
2. 其可以被定義在另外一個函數內
這也就是說,函數可以返回一個函數,看下面的例子:

def getTalk(type="shout") :

# 我們定義另外一個函數
def shout(word="yes") :
return word.capitalize()+" !"

def whisper(word="yes") :
return word.lower()+"...";

# 然後我們返回其中一個
if type == "shout" :
# 我們沒有使用(),因為我們不是在調用該函數
# 我們是在返回該函數
return shout
else :
return whisper

# 然後怎麼使用呢 ?

# 把該函數賦予某個變數
talk = getTalk()

# 這里你可以看到talk其實是一個函數對象:
print talk
#輸出 : <function shout at 0xb7ea817c>

# 該對象由函數返回的其中一個對象:
print talk()

# 或者你可以直接如下調用 :
print getTalk("whisper")()
#輸出 : yes...

還有,既然可以返回一個函數,我們可以把它作為參數傳遞給函數:

def doSomethingBefore(func) :
print "I do something before then I call the function you gave me"
print func()

doSomethingBefore(scream)
#輸出 :
#I do something before then I call the function you gave me
#Yes !

這里你已經足夠能理解裝飾器了,其他它可被視為封裝器。也就是說,它能夠讓你在裝飾前後執行代碼而無須改變函數本身內容。
手工裝飾
那麼如何進行手動裝飾呢?

# 裝飾器是一個函數,而其參數為另外一個函數
def my_shiny_new_decorator(a_function_to_decorate) :

# 在內部定義了另外一個函數:一個封裝器。
# 這個函數將原始函數進行封裝,所以你可以在它之前或者之後執行一些代碼
def the_wrapper_around_the_original_function() :

# 放一些你希望在真正函數執行前的一些代碼
print "Before the function runs"

# 執行原始函數
a_function_to_decorate()

# 放一些你希望在原始函數執行後的一些代碼
print "After the function runs"

#在此刻,"a_function_to_decrorate"還沒有被執行,我們返回了創建的封裝函數
#封裝器包含了函數以及其前後執行的代碼,其已經准備完畢
return the_wrapper_around_the_original_function

# 現在想像下,你創建了一個你永遠也不遠再次接觸的函數
def a_stand_alone_function() :
print "I am a stand alone function, don't you dare modify me"

a_stand_alone_function()
#輸出: I am a stand alone function, don't you dare modify me

# 好了,你可以封裝它實現行為的擴展。可以簡單的把它丟給裝飾器
# 裝飾器將動態地把它和你要的代碼封裝起來,並且返回一個新的可用的函數。
a_stand_alone_function_decorated = my_shiny_new_decorator(a_stand_alone_function)
a_stand_alone_function_decorated()
#輸出 :
#Before the function runs
#I am a stand alone function, don't you dare modify me
#After the function runs

現在你也許要求當每次調用a_stand_alone_function時,實際調用卻是a_stand_alone_function_decorated。實現也很簡單,可以用my_shiny_new_decorator來給a_stand_alone_function重新賦值。

a_stand_alone_function = my_shiny_new_decorator(a_stand_alone_function)
a_stand_alone_function()
#輸出 :
#Before the function runs
#I am a stand alone function, don't you dare modify me
#After the function runs

# And guess what, that's EXACTLY what decorators do !

裝飾器揭秘
前面的例子,我們可以使用裝飾器的語法:

@my_shiny_new_decorator
def another_stand_alone_function() :
print "Leave me alone"

another_stand_alone_function()
#輸出 :
#Before the function runs
#Leave me alone
#After the function runs

當然你也可以累積裝飾:

def bread(func) :
def wrapper() :
print "</''''''\>"
func()
print "<\______/>"
return wrapper

def ingredients(func) :
def wrapper() :
print "#tomatoes#"
func()
print "~salad~"
return wrapper

def sandwich(food="--ham--") :
print food

sandwich()
#輸出 : --ham--
sandwich = bread(ingredients(sandwich))
sandwich()
#outputs :
#</''''''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>

使用python裝飾器語法:

@bread
@ingredients
def sandwich(food="--ham--") :
print food

sandwich()
#輸出 :
#</''''''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>

『肆』 Python如果一個父類已經實例化了,現在想新建一個子類,給父類的這一個實例添加兩個屬性,如何實現

class People(object):
def __init__(self, name, age):
self.name = name
self.age = age
class worker(People):
def __init__(self,name,age,salary):
super(worker,self).__init__(name,age)
self.salary = salary
tom = People("Tom", 22)
print type(tom).__name__
#not a safe way, but no new object
tom.__class__=worker
print type(tom).__name__
tom.salary = 250
print tom.salary
#safe way, but create a new object
workerTom = worker("Tom", 22, 200)
tom.__dict__ = workerTom.__dict__
print type(tom).__name__
print tom.salary

『伍』 Python有設計模式么

Python設計模式主要分為三大類:創建型模式、結構型模式、行為型模式;三 大類中又被細分為23種設計模式,以下這幾種是最常見的。
單例模式:是一種常用的軟體設計模式,該模式的主要目的是確保某一個類只有一個實例存在。當你希望在整個系統中,某個類只能出現一個是實例時,單例對象就能派上用場。單例對象的要點有三個:一是某個類只能有一個實例;二是它必須自行創建整個實例,三是它必須自行向整個系統提供這個實例。
工廠模式:提供一個創建對象的介面,不像客戶端暴露創建對象的過程,使用一個公共的介面來創建對象,可以分為三種:簡單工廠、工廠方法、抽象工廠。一個類的行為或其演算法可以在運行時更改,這種類型的設計模式屬於行為型模式。
策略模式:是常見的設計模式之一,它是指對一系列的演算法定義,並將每一個演算法封裝起來,而且使它們還可以相互替換。策略模式讓演算法獨立於使用它的客戶而獨立變化。換句話來講,就是針對一個問題而定義出一個解決的模板,這個模板就是具體的策略,每個策略都是按照這個模板進行的,這種情況下我們有新的策略時就可以直接按照模板來寫,而不會影響之前已經定義好的策略。
門面模式:門面模式也被稱作外觀模式。定義如下:要求一個子系統的外部與其內部的通信必須通過一個統一的對象進行。門面模式提供一個高層次的介面,使得子系統更易於使用。門面模式注重統一的對象,也就是提供一個訪問子系統的介面。門面模式與模板模式有相似的地方,都是對一些需要重復方法的封裝。但本質上是不同的,模板模式是對類本身的方法的封裝,其被封裝的方法也可以單獨使用;門面模式,是對子系統的封裝,其被封裝的介面理論上是不會被單獨提出來使用的。

『陸』 Python中裝飾器為什麼只是第一次執行被裝飾函數的時候才調用

可以的啦#-*-coding:UTF-8-*-fromfunctoolsimportwraps__author__='lpe234'defsingleton(cls):"""裝飾器實現單例模式:paramcls::return:"""instances={}@wraps(cls)def_singleton(*args,**kwargs):ifclsnotininstances:instances[cls]=cls(*args,**kwargs)returninstances[cls]return_singleton@singletonclassSelfClass(object):passdefmain():s1=SelfClass()s2=SelfClass()asserts1iss2if__name__=='__main__':main()

『柒』 PYTHON里的裝飾器能裝飾類嗎

可以的啦

#-*-coding:UTF-8-*-
fromfunctoolsimportwraps

__author__='lpe234'


defsingleton(cls):
"""
裝飾器實現單例模式
:paramcls:
:return:
"""
instances={}

@wraps(cls)
def_singleton(*args,**kwargs):
ifclsnotininstances:
instances[cls]=cls(*args,**kwargs)
returninstances[cls]
return_singleton


@singleton
classSelfClass(object):
pass


defmain():
s1=SelfClass()
s2=SelfClass()
asserts1iss2

if__name__=='__main__':
main()

『捌』 網頁頁面設計過程中python設計如何與其結合,代碼植入方法與核心技術流程舉例

摘要 您好。

『玖』 Python 有什麼奇技淫巧

Python奇技淫巧
當發布python第三方package時, 並不希望代碼中所有的函數或者class可以被外部import, 在 __init__.py 中添加 __all__ 屬性,

該list中填寫可以import的類或者函數名, 可以起到限制的import的作用, 防止外部import其他函數或者類

#!/usr/bin/env python
# -*- coding: utf-8 -*-

frombaseimportAPIBase
fromclientimportClient
fromdecoratorimportinterface, export, stream
fromserverimportServer
fromstorageimportStorage
fromutilimport(LogFormatter, disable_logging_to_stderr,
enable_logging_to_kids, info)

__all__ = ['APIBase','Client','LogFormatter','Server',
'Storage','disable_logging_to_stderr','enable_logging_to_kids',
'export','info','interface','stream']

with的魔力

with語句需要支持 上下文管理協議的對象 , 上下文管理協議包含 __enter__ 和 __exit__ 兩個方法. with語句建立運行時上下文需要通過這兩個方法執行 進入和退出 操作.

其中 上下文表達式 是跟在with之後的表達式, 該表示大返回一個上下文管理對象
# 常見with使用場景
withopen("test.txt","r")asmy_file:# 注意, 是__enter__()方法的返回值賦值給了my_file,
forlineinmy_file:
print line

詳細原理可以查看這篇文章, 淺談 Python 的 with 語句

知道具體原理, 我們可以自定義支持上下文管理協議的類, 類中實現 __enter__ 和 __exit__ 方法
#!/usr/bin/env python
# -*- coding: utf-8 -*-

classMyWith(object):

def__init__(self):
print"__init__ method"

def__enter__(self):
print"__enter__ method"
returnself# 返回對象給as後的變數

def__exit__(self, exc_type, exc_value, exc_traceback):
print"__exit__ method"
ifexc_tracebackisNone:
print"Exited without Exception"
returnTrue
else:
print"Exited with Exception"
returnFalse

deftest_with():
withMyWith()asmy_with:
print"running my_with"
print"------分割線-----"
withMyWith()asmy_with:
print"running before Exception"
raiseException
print"running after Exception"

if__name__ =='__main__':
test_with()

執行結果如下:
__init__ method
__enter__ method
running my_with
__exit__ method
ExitedwithoutException
------分割線-----
__init__ method
__enter__ method
running before Exception
__exit__ method
ExitedwithException
Traceback(most recent call last):
File"bin/python", line34,in<mole>
exec(compile(__file__f.read(), __file__, "exec"))
File"test_with.py", line33,in<mole>
test_with()
File"test_with.py", line28,intest_with
raiseException
Exception

證明了會先執行 __enter__ 方法, 然後調用with內的邏輯, 最後執行 __exit__ 做退出處理, 並且, 即使出現異常也能正常退出

filter的用法

相對 filter 而言, map和rece使用的會更頻繁一些, filter 正如其名字, 按照某種規則 過濾 掉一些元素
#!/usr/bin/env python
# -*- coding: utf-8 -*-

lst = [1,2,3,4,5,6]
# 所有奇數都會返回True, 偶數會返回False被過濾掉
print filter(lambda x: x % 2!=0, lst)

#輸出結果
[1,3,5]

一行作判斷

當條件滿足時, 返回的為等號後面的變數, 否則返回else後語句
lst = [1,2,3]
new_lst = lst[0]iflstisnotNoneelseNone
printnew_lst

# 列印結果
1

裝飾器之單例

使用裝飾器實現簡單的單例模式

# 單例裝飾器
defsingleton(cls):
instances = dict() # 初始為空
def_singleton(*args, **kwargs):
ifclsnotininstances:#如果不存在, 則創建並放入字典
instances[cls] = cls(*args, **kwargs)
returninstances[cls]
return_singleton

@singleton
classTest(object):
pass

if__name__ =='__main__':
t1 = Test()
t2 = Test()
# 兩者具有相同的地址
printt1, t2

staticmethod裝飾器

類中兩種常用的裝飾, 首先區分一下他們

普通成員函數, 其中第一個隱式參數為 對象
classmethod裝飾器 , 類方法(給人感覺非常類似於OC中的類方法), 其中第一個隱式參數為 類
staticmethod裝飾器 , 沒有任何隱式參數. python中的靜態方法類似與C++中的靜態方法
#!/usr/bin/env python
# -*- coding: utf-8 -*-

classA(object):

# 普通成員函數
deffoo(self, x):
print "executing foo(%s, %s)"% (self, x)

@classmethod# 使用classmethod進行裝飾
defclass_foo(cls, x):
print "executing class_foo(%s, %s)"% (cls, x)

@staticmethod# 使用staticmethod進行裝飾
defstatic_foo(x):
print "executing static_foo(%s)"% x

deftest_three_method():
obj = A()
# 直接調用噗通的成員方法
obj.foo("para")# 此處obj對象作為成員函數的隱式參數, 就是self
obj.class_foo("para")# 此處類作為隱式參數被傳入, 就是cls
A.class_foo("para")#更直接的類方法調用
obj.static_foo("para")# 靜態方法並沒有任何隱式參數, 但是要通過對象或者類進行調用
A.static_foo("para")

if__name__=='__main__':
test_three_method()

# 函數輸出
executing foo(<__main__.Aobject at0x100ba4e10>, para)
executing class_foo(<class'__main__.A'>,para)
executing class_foo(<class'__main__.A'>,para)
executing static_foo(para)
executing static_foo(para)

property裝飾器

定義私有類屬性

將 property 與裝飾器結合實現屬性私有化( 更簡單安全的實現get和set方法 )
#python內建函數
property(fget=None, fset=None, fdel=None, doc=None)

fget 是獲取屬性的值的函數, fset 是設置屬性值的函數, fdel 是刪除屬性的函數, doc 是一個字元串(like a comment).從實現來看,這些參數都是可選的

property有三個方法 getter() , setter() 和 delete() 來指定fget, fset和fdel。 這表示以下這行
classStudent(object):

@property #相當於property.getter(score) 或者property(score)
defscore(self):
returnself._score

@score.setter #相當於score = property.setter(score)
defscore(self, value):
ifnotisinstance(value, int):
raiseValueError('score must be an integer!')
ifvalue <0orvalue >100:
raiseValueError('score must between 0 ~ 100!')
self._score = value

iter魔法

通過yield和 __iter__ 的結合, 我們可以把一個對象變成可迭代的
通過 __str__ 的重寫, 可以直接通過想要的形式列印對象
#!/usr/bin/env python
# -*- coding: utf-8 -*-

classTestIter(object):

def__init__(self):
self.lst = [1,2,3,4,5]

defread(self):
foreleinxrange(len(self.lst)):
yieldele

def__iter__(self):
returnself.read()

def__str__(self):
return','.join(map(str, self.lst))

__repr__ = __str__

deftest_iter():
obj = TestIter()
fornuminobj:
printnum
printobj

if__name__ =='__main__':
test_iter()

神奇partial

partial使用上很像C++中仿函數(函數對象).

在stackoverflow給出了類似與partial的運行方式
defpartial(func, *part_args):
defwrapper(*extra_args):
args = list(part_args)
args.extend(extra_args)
returnfunc(*args)

returnwrapper

利用用閉包的特性綁定預先綁定一些函數參數, 返回一個可調用的變數, 直到真正的調用執行
#!/usr/bin/env python
# -*- coding: utf-8 -*-

fromfunctoolsimportpartial

defsum(a, b):
returna + b

deftest_partial():
fun = partial(sum, 2)# 事先綁定一個參數, fun成為一個只需要一個參數的可調用變數
printfun(3)# 實現執行的即是sum(2, 3)

if__name__ =='__main__':
test_partial()

# 執行結果
5

神秘eval

eval我理解為一種內嵌的python解釋器(這種解釋可能會有偏差), 會解釋字元串為對應的代碼並執行, 並且將執行結果返回

看一下下面這個例子
#!/usr/bin/env python
# -*- coding: utf-8 -*-

deftest_first():
return3

deftest_second(num):
returnnum

action = { # 可以看做是一個sandbox
"para":5,
"test_first": test_first,
"test_second": test_second
}

deftest_eavl():
condition = "para == 5 and test_second(test_first) > 5"
res = eval(condition, action) # 解釋condition並根據action對應的動作執行
printres

if__name__ =='_

exec

exec在Python中會忽略返回值, 總是返回None, eval會返回執行代碼或語句的返回值
exec 和 eval 在執行代碼時, 除了返回值其他行為都相同
在傳入字元串時, 會使用 compile(source, '<string>', mode) 編譯位元組碼. mode的取值為 exec 和 eval
#!/usr/bin/env python
# -*- coding: utf-8 -*-

deftest_first():
print"hello"

deftest_second():
test_first()
print"second"

deftest_third():
print"third"

action = {
"test_second": test_second,
"test_third": test_third
}

deftest_exec():
exec"test_second"inaction

if__name__ =='__main__':
test_exec() # 無法看到執行結果

getattr

getattr(object, name[, default]) Return the value of
the named attribute of object. name must be a string. If the string is
the name of one of the object』s attributes, the result is the value of
that attribute. For example, getattr(x, 『foobar』) is equivalent to
x.foobar. If the named attribute does not exist, default is returned if
provided, otherwise AttributeError is raised.

通過string類型的name, 返回對象的name屬性(方法)對應的值, 如果屬性不存在, 則返回默認值, 相當於object.name
# 使用範例
classTestGetAttr(object):

test = "test attribute"

defsay(self):
print"test method"

deftest_getattr():
my_test = TestGetAttr()
try:
printgetattr(my_test,"test")
exceptAttributeError:
print"Attribute Error!"
try:
getattr(my_test, "say")()
exceptAttributeError:# 沒有該屬性, 且沒有指定返回值的情況下
print"Method Error!"

if__name__ =='__main__':
test_getattr()

# 輸出結果
test attribute
test method

命令行處理
defprocess_command_line(argv):
"""
Return a 2-tuple: (settings object, args list).
`argv` is a list of arguments, or `None` for ``sys.argv[1:]``.
"""
ifargvisNone:
argv = sys.argv[1:]

# initialize the parser object:
parser = optparse.OptionParser(
formatter=optparse.TitledHelpFormatter(width=78),
add_help_option=None)

# define options here:
parser.add_option( # customized description; put --help last
'-h','--help', action='help',
help='Show this help message and exit.')

settings, args = parser.parse_args(argv)

# check number of arguments, verify values, etc.:
ifargs:
parser.error('program takes no command-line arguments; '
'"%s" ignored.'% (args,))

# further process settings & args if necessary

returnsettings, args

defmain(argv=None):
settings, args = process_command_line(argv)
# application code here, like:
# run(settings, args)
return0# success

if__name__ =='__main__':
status = main()
sys.exit(status)

讀寫csv文件
# 從csv中讀取文件, 基本和傳統文件讀取類似
importcsv
withopen('data.csv','rb')asf:
reader = csv.reader(f)
forrowinreader:
printrow
# 向csv文件寫入
importcsv
withopen('data.csv','wb')asf:
writer = csv.writer(f)
writer.writerow(['name','address','age'])# 單行寫入
data = [
( 'xiaoming ','china','10'),
( 'Lily','USA','12')]
writer.writerows(data) # 多行寫入

各種時間形式轉換

只發一張網上的圖, 然後差文檔就好了, 這個是記不住的

字元串格式化

一個非常好用, 很多人又不知道的功能
>>>name ="andrew"
>>>"my name is {name}".format(name=name)
'my name is andrew'

閱讀全文

與裝飾器模式python相關的資料

熱點內容
伺服器旁為什麼要有電腦 瀏覽:522
什麼樣的app上買機票最便宜 瀏覽:987
安卓如何查看異常重啟 瀏覽:717
解壓音樂排名 瀏覽:386
安卓手機瀏覽器怎麼掃二維碼 瀏覽:720
通達信成本均線源碼 瀏覽:619
可以下載的解壓音頻 瀏覽:567
海賊王怎麼換伺服器 瀏覽:321
計算機上的共享文件夾映射 瀏覽:943
榮耀安裝包在文件夾哪裡 瀏覽:198
機票php源碼 瀏覽:235
linux共享mac 瀏覽:926
中國沒有國外的伺服器地址 瀏覽:761
為什麼退款伺服器連接錯誤 瀏覽:559
android簡訊存儲位置 瀏覽:977
unix網路編程卷4 瀏覽:808
找靚機app下單什麼時候發貨 瀏覽:413
android一個應用兩個進程 瀏覽:803
linux硬碟復制 瀏覽:808
php圖片伺服器搭建 瀏覽:801