摘要:問題中鮮為人知的特性一討論函數(shù)參數(shù)鏈?zhǔn)奖容^操作符注意函數(shù)的默認(rèn)參數(shù)更安全的做法帶關(guān)鍵字的格式化語法的特殊方法當(dāng)查找不到的時(shí)候,會(huì)執(zhí)行這個(gè)方法。
問題
Python中鮮為人知的特性(一)
討論函數(shù)參數(shù)unpack
def foo(x, y): print x, y alist = [1, 2] adict = {"x": 1, "y": 2} foo(*alist) # 1, 2 foo(**adict) # 1, 2
鏈?zhǔn)奖容^操作符
>>> x = 3 >>> 1 < x < 5 True >>> 4 > x >=3 True
注意函數(shù)的默認(rèn)參數(shù)
>>> def foo(x=[]): ... x.append(1) ... print x ... >>> foo() [1] >>> foo() [1, 1]
更安全的做法:
>>> def foo(x=None): ... if x is None: ... x = [] ... x.append(1) ... print x ... >>> foo() [1] >>> foo() [1] >>>
帶關(guān)鍵字的格式化
>>> print "Hello {name} !".format(name="James") Hello James !
for...else 語法
>>> for i in (1, 3, 5): ... if i % 2 == 0: ... break ... else: ... print "var i is always an odd" ... var i is always an odd >>>
dict 的特殊方法__missing__
當(dāng)查找不到 key 的時(shí)候,會(huì)執(zhí)行這個(gè)方法。
>>> class Dict(dict): ... def __missing__(self, key): ... self[key] = [] ... return self[key] ... >>> dct = Dict() >>> dct["foo"].append(1) >>> dct["foo"].append(2) >>> dct["foo"] [1, 2]
切片操作的步長參數(shù)
>>> a = [1, 2, 3, 4, 5] >>> a[::2] [1, 3, 5] >>> a[::-1] [5, 4, 3, 2, 1]
Python解釋器中的”_”
_ 即Python解釋器上一次返回的值
>>> range(4) [0, 1, 2, 3] >>> _ [0, 1, 2, 3]
Python之禪
>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren"t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you"re Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it"s a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let"s do more of those!來源
PyZh
關(guān)注歡迎關(guān)注我的微信公眾號:python每日一練
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/41663.html
Python元素的內(nèi)容還是比較的復(fù)雜的,里面包含有很多的不同特性,不同特性之間,其用法也是不一樣的。那么,其中它的三個(gè)不同特性之間,里面的內(nèi)容是什么呢?下面就給大家詳細(xì)的解答下?! ?.引言 元組是Python中一種重要的內(nèi)置數(shù)據(jù)類型。與列表一樣,我們經(jīng)常使用元組將多個(gè)對象保存為相應(yīng)的數(shù)據(jù)容器。然而,與列表不同的是元組的不變性——一個(gè)不可改變的數(shù)據(jù)序列?! ?.舉個(gè)例子 下面的代碼片段向我...
摘要:有著一堆神秘的語法和過時(shí)的功能。我試圖列出一些鮮為人知的特性。雖然它們很酷,但畢竟是鮮為人知的特性,你的同事可能會(huì)看不懂。類似這樣使用的話會(huì)始終保持返回正確的。 By Viral Shah | Nov 26, 2018 原文 js一門很容易入門但是很難精通的語言。我很認(rèn)同這句話。這是因?yàn)閖s是一門古老的語言同時(shí)也是一門很靈活的語言。有著一堆神秘的語法和過時(shí)的功能。我已經(jīng)使用js很多年了...
摘要:文中的我指原文作者通常被認(rèn)為是最容易入門的語言,也是最難掌握的語言,我完全同意。這是因?yàn)槭且环N非常古老且非常靈活的語言,它有著了神秘的語法和過時(shí)的特性。雖然這些特性可能不太為人所知,但它們?nèi)匀皇潜娝苤摹? 文中的 我 指原文作者 javaScript 通常被認(rèn)為是最容易入門的語言,也是最難掌握的語言,我完全同意。這是因?yàn)?JavaScript 是一種非常古老且非常靈活的語言,它有著了...
摘要:文中的我指原文作者通常被認(rèn)為是最容易入門的語言,也是最難掌握的語言,我完全同意。這是因?yàn)槭且环N非常古老且非常靈活的語言,它有著了神秘的語法和過時(shí)的特性。雖然這些特性可能不太為人所知,但它們?nèi)匀皇潜娝苤摹? 文中的 我 指原文作者 javaScript 通常被認(rèn)為是最容易入門的語言,也是最難掌握的語言,我完全同意。這是因?yàn)?JavaScript 是一種非常古老且非常靈活的語言,它有著了...
閱讀 2125·2023-04-26 00:41
閱讀 1159·2021-09-24 10:34
閱讀 3585·2021-09-23 11:21
閱讀 4135·2021-09-22 15:06
閱讀 1569·2019-08-30 15:55
閱讀 909·2019-08-30 15:54
閱讀 1837·2019-08-30 15:48
閱讀 564·2019-08-29 13:58