Python奇技淫巧你知道多少?
點(diǎn)擊關(guān)注上方“SQL數(shù)據(jù)庫開發(fā)”,
設(shè)為“置頂或星標(biāo)”,第一時(shí)間送達(dá)干貨
顯示有限的接口到外部
__init__.py中添加__all__屬性,該list中填寫可以import的類或者函數(shù)名, 可以起到限制的import的作用, 防止外部import其他函數(shù)或者類


證明了會(huì)先執(zhí)行 __enter__方法, 然后調(diào)用with內(nèi)的邏輯, 最后執(zhí)行__exit__做退出處理, 并且, 即使出現(xiàn)異常也能正常退出
filter的用法
filter而言, map和reduce使用的會(huì)更頻繁一些,?filter正如其名字, 按照某種規(guī)則過濾掉一些元素
一行作判斷

裝飾器之單例

staticmethod裝飾器
普通成員函數(shù), 其中第一個(gè)隱式參數(shù)為 對(duì)象classmethod裝飾器, 類方法(給人感覺非常類似于OC中的類方法), 其中第一個(gè)隱式參數(shù)為類staticmethod裝飾器, 沒有任何隱式參數(shù).?python中的靜態(tài)方法類似與C++中的靜態(tài)方法

property裝飾器
定義私有類屬性
property與裝飾器結(jié)合實(shí)現(xiàn)屬性私有化(更簡單安全的實(shí)現(xiàn)get和set方法)fget是獲取屬性的值的函數(shù),fset是設(shè)置屬性值的函數(shù),fdel是刪除屬性的函數(shù),doc是一個(gè)字符串(like a comment).從實(shí)現(xiàn)來看,這些參數(shù)都是可選的getter(),?setter()和delete()?來指定fget, fset和fdel。這表示以下這行
iter魔法
通過yield和 __iter__的結(jié)合, 我們可以把一個(gè)對(duì)象變成可迭代的通過 __str__的重寫, 可以直接通過想要的形式打印對(duì)象

神奇partial


神秘eval

exec
exec在Python中會(huì)忽略返回值, 總是返回None, eval會(huì)返回執(zhí)行代碼或語句的返回值 exec和eval在執(zhí)行代碼時(shí), 除了返回值其他行為都相同在傳入字符串時(shí), 會(huì)使用 compile(source, '編譯字節(jié)碼. mode的取值為', mode) exec和eval

etattr
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.

命令行處理

讀寫csv文件

各種時(shí)間形式轉(zhuǎn)換

字符串格式化
作者:code123
出處:http://www.code123.cc/2285.html
聲明:文章著作權(quán)歸作者所有,如有侵權(quán),請(qǐng)聯(lián)系小編刪除
——End——
后臺(tái)回復(fù)關(guān)鍵字:1024,獲取一份精心整理的技術(shù)干貨 后臺(tái)回復(fù)關(guān)鍵字:進(jìn)群,帶你進(jìn)入高手如云的交流群。 推薦閱讀
這是一個(gè)能學(xué)到技術(shù)的公眾號(hào),歡迎關(guān)注
評(píng)論
圖片
表情
