绗洓绔?闈㈠悜瀵硅薄
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绗洓绔?闈㈠悜瀵硅薄相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/dict' title='dict'>dict
mem port color big obj 鐢ㄦ硶 exit 浠g爜鐗囨1. 绠€杩伴潰鍚戝璞$殑涓夊ぇ鐗规€с€?/strong>
銆€銆€鍙傝€冮摼鎺?/a>
銆€銆€缁ф壙锛屽皝瑁呭拰澶氭€?/em>
- 缁ф壙锛?
- 缁ф壙灏辨槸缁ф壙鐨勭被鐩存帴鎷ユ湁琚户鎵跨被鐨勫睘鎬ц€屼笉闇€瑕佸湪鑷繁鐨勭被浣撲腑閲嶆柊鍐嶅啓涓€閬嶏紝鍏朵腑琚户鎵跨殑绫诲彨鍋氱埗绫汇€佸熀绫伙紝缁ф壙鐨勭被鍙仛娲剧敓绫汇€佸瓙绫汇€?/li>
- 灏佽锛?
- 灏佽灏辨槸鎶婄被涓殑灞炴€у拰鏂规硶瀹氫箟涓虹鏈夌殑锛屾柟娉曞氨鏄湪灞炴€у悕鎴栨柟娉曞悕鍓嶅姞鍙屼笅鍒掔嚎锛岃€屼竴鏃﹁繖鏍峰畾涔変簡灞炴€ф垨鏂规硶鍚嶅悗锛宲ython浼氳嚜鍔ㄥ皢鍏惰浆鎹负_绫诲悕__灞炴€у悕锛堟柟娉曞悕锛夌殑鏍煎紡锛屽湪绫荤殑鍐呴儴璋冪敤杩樻槸鐢ㄥ弻涓嬪垝绾垮姞灞炴€у悕鎴栨柟娉曞悕锛屽湪绫荤殑澶栭儴璋冪敤灏辫鐢╛绫诲悕__灞炴€у悕锛堟柟娉曞悕锛夈€傜埗绫荤殑绉佹湁灞炴€у拰鏂规硶锛屽瓙绫绘棤娉曞鍏惰繘琛屼慨鏀广€?/li>
- 澶氭€侊細
- 澶氭€佸氨鏄笉鍚岀殑瀵硅薄鍙互璋冪敤鐩稿悓鐨勬柟娉曠劧鍚庡緱鍒颁笉鍚岀殑缁撴灉锛屾湁鐐圭被浼兼帴鍙g被鐨勬劅瑙夛紝鍦╬ython涓澶勪綋鐜扮潃澶氭€侊紝姣斿涓嶇浣犳槸鍒楄〃杩樻槸瀛楃涓茶繕鏄暟瀛楅兘鍙互浣跨敤+鍜?銆?/li>
2. 浠€涔堟槸楦瓙妯″瀷锛?/strong>
銆€銆€楦瓙绫诲瀷锛堣嫳璇細duck typing锛夋槸鍔ㄦ€佺被鍨嬬殑涓€绉嶉鏍笺€傚湪杩欑椋庢牸涓紝涓€涓璞℃湁鏁堢殑璇箟锛屼笉鏄敱缁ф壙鑷壒瀹氱殑绫绘垨瀹炵幇鐗瑰畾鐨勬帴鍙o紝鑰屾槸鐢卞綋鍓嶆柟娉曞拰灞炴€х殑闆嗗悎鍐冲畾銆?/em>濡傛灉鍏锋湁__iter__,__next__灏辨槸杩唬鍣?/p>
3. super 鐨勪綔鐢紵
銆€褰撳瓙绫讳腑鐨勬柟娉曚笌鐖剁被涓殑鏂规硶閲嶅悕鏃讹紝瀛愮被涓殑鏂规硶浼氳鐩栫埗绫讳腑鐨勬柟娉曪紝閭d箞锛屽鏋滄垜浠兂瀹炵幇鍚屾椂璋冪敤鐖剁被鍜屽瓙绫讳腑鐨勫悓鍚嶆柟娉曪紝灏遍渶瑕佷娇鐢ㄥ埌super()杩欎釜鍑芥暟锛岀敤娉曚负super().鍑芥暟鍚?)
4. mro 鏄粈涔堬紵
銆€銆€绫诲悕.mro()鍙互鐩存帴鍒犻櫎褰撳墠绫荤殑缁ф壙鎵ц椤哄簭
- 瀵逛簬鏀寔缁ф壙鐨勭紪绋嬭瑷€鏉ヨ锛屽叾鏂规硶锛堝睘鎬э級鍙兘瀹氫箟鍦ㄥ綋鍓嶇被锛屼篃鍙兘鏉ヨ嚜浜庡熀绫伙紝鎵€浠ュ湪鏂规硶璋冪敤鏃跺氨闇€瑕佸褰撳墠绫诲拰鍩虹被杩涜鎼滅储浠ョ‘瀹氭柟娉曟墍鍦ㄧ殑浣嶇疆銆傝€屾悳绱㈢殑椤哄簭灏辨槸鎵€璋撶殑銆屾柟娉曡В鏋愰『搴忋€嶏紙Method Resolution Order锛屾垨MRO锛夈€?/li>
5. 浠€涔堟槸 c3 绠楁硶锛?/strong>
銆€銆€c3绠楁硶鏄痯ython鏂板紡绫讳腑鐢ㄦ潵浜х敓mro椤哄簭鐨勪竴濂楃畻娉曘€傚嵆澶氱户鎵跨殑鏌ユ壘瑙勫垯銆?/em>
6. 鍒椾妇闈㈠悜瀵硅薄涓甫鍙屼笅鍒掔嚎鐨勭壒娈婃柟娉曘€?/strong>
銆€銆€__new__锛氬彲浠ヨ皟鐢ㄥ叾瀹冪被鐨勬瀯閫犳柟娉曟垨鑰呯洿鎺ヨ繑鍥炲埆鐨勫璞℃潵浣滀负鏈被鐨勫疄渚嬨€?/em>
銆€銆€ __init__锛?璐熻矗绫荤殑瀹炰緥鍖?/em>
銆€銆€ __call__锛氬璞″悗杈瑰姞鎷彿锛岃Е鍙戞墽琛?
銆€銆€__str__锛歱rint鎵撳嵃涓€涓璞℃椂銆?/em>
銆€銆€__repr__:print鎵撳嵃涓€涓璞℃椂(娌℃湁__str__鏃舵墽琛?銆?/em>
銆€銆€__enter__涓巁_exit__锛氶厤鍚坵ith浣跨敤銆?/em>
銆€銆€__doc__锛氱被鐨勬敞閲婏紝璇ュ睘鎬ф槸鏃犳硶缁ф壙鐨勩€?
銆€銆€__getattr__锛氬湪浣跨敤璋冪敤灞炴€э紙鏂瑰紡銆佸睘鎬э級涓嶅瓨鍦ㄧ殑鏃跺€欒Е鍙?
銆€銆€__setattr__锛氭坊鍔?淇敼灞炴€т細瑙﹀彂瀹冪殑鎵ц
銆€銆€ __dellattr__锛氬垹闄ゅ睘鎬х殑鏃跺€欎細瑙﹀彂
銆€銆€__delete__锛氶噰鐢╠el鍒犻櫎灞炴€ф椂锛岃Е鍙?/em>
7. 鍙屼笅鍒掔嚎鍜屽崟涓嬪垝绾跨殑鍖哄埆锛?/strong>
- "鍗曚笅鍒掔嚎" 寮€濮嬬殑鎴愬憳鍙橀噺鍙仛淇濇姢鍙橀噺锛屾剰鎬濇槸鍙湁绫诲璞″拰瀛愮被瀵硅薄鑷繁鑳借闂埌杩欎簺鍙橀噺锛?/em>
- "鍙屼笅鍒掔嚎" 寮€濮嬬殑鏄鏈夋垚鍛橈紝鎰忔€濇槸鍙湁绫诲璞¤嚜宸辫兘璁块棶锛岃繛瀛愮被瀵硅薄涔熶笉鑳借闂埌杩欎釜鏁版嵁銆?/em>
8. 瀹炰緥鍙橀噺鍜岀被鍙橀噺鐨勫尯鍒紵
- 瀹炰緥鍙橀噺鏄浜庢瘡涓疄渚嬮兘鐙湁鐨勬暟鎹?/em>
- 绫诲彉閲忔槸璇ョ被鎵€鏈夊疄渚嬪叡浜殑灞炴€у拰鏂规硶
9. 闈欐€佹柟娉曞拰绫绘柟娉曞尯鍒紵
- 瀹炰緥鏂规硶锛氱涓€涓弬鏁板繀椤绘槸瀹炰緥瀵硅薄锛岄€氬父涓簊elf銆傚疄渚嬫柟娉曞彧鑳界敱瀹炰緥瀵硅薄璋冪敤銆?/em>
- 绫绘柟娉曪細浣跨敤瑁呴グ鍣ˊclassmethod銆傜涓€涓弬鏁颁负褰撳墠绫荤殑瀵硅薄锛岄€氬父涓篶ls銆傚疄渚嬪璞″拰绫诲璞¢兘鍙互璋冪敤绫绘柟娉曘€?/em>
- 闈欐€佹柟娉曪細浣跨敤瑁呴グ鍣ˊstaticmethod銆傛病鏈塻elf鍜宑ls鍙傛暟銆傛柟娉曚綋涓笉鑳戒娇鐢ㄧ被鎴栬€呭疄渚嬬殑浠讳綍灞炴€у拰鏂规硶銆傚疄渚嬪璞″拰绫诲璞¢兘鍙互璋冪敤銆?/em>
10. isinstance 鍜?type 鐨勪綔鐢紵
- 涓よ€呴兘鐢ㄦ潵鍒ゆ柇瀵硅薄鐨勭被鍨?/em>
- 瀵逛簬涓€涓璞$殑鐩村睘绫诲瀷鍒ゆ柇鍙互浣跨敤type锛屼絾鏄潪鐩村睘绫诲瀷鍒ゆ柇灏遍渶瑕乮sinstance
11. 鏈夌敤杩?with statement锛堣鍙ワ級鍚楋紵瀹冪殑濂藉鏄粈涔堬紵
瀛楁鑻辨枃鍚?/p> |
涓枃鍚?/p> |
瀛楁绫诲瀷 |
鍙栧€?/p> |
Id |
涓婚敭 |
Long |
123456789 |
Name |
濮撳悕 |
String |
寮犱笁 |
Items |
鍟嗗搧鍒楄〃闆嗗悎 |
List<鍟嗗搧>(鍏宠仈鍟嗗搧) |
鏌ユ壘鍟嗗搧瀵硅薄锛屼竴涓鍗曟湁涓や釜鍟嗗搧銆傚晢鍝佸瓧娈典换鎰忓彇鍊?/p> |
IsMember |
鏄惁鏄細鍛?/p> |
Boolean |
True |
CouponAmount |
浼樻儬鍒搁噾棰?/p> |
Bigdecimal |
Null |
銆€銆€
銆€銆€銆€銆€鍟嗗搧瀵硅薄
瀛楁鑻辨枃鍚嶇О |
涓枃鍚?/td> | 瀛楁绫诲瀷 | 鍙栧€?/td> |
Id | 涓婚敭 | Long | 987654321 |
Name | 鍟嗗搧鍚嶇О | String | 鎵嬫満 |
銆€銆€銆€銆€闂锛氳嫢灏嗚鍗曞璞¤浆鎴?JSON 鏍煎紡锛岃涔﹀啓鍑鸿浆鎹㈠悗鐨?JSON 瀛楃涓层€?/p>
28. 鍐欎唬鐮?鏍堜笌闃熷垪)
銆€銆€缂栫▼瀹炵幇涓€涓厛杩涘厛鍑虹殑闃熷垪绫? 鑳芥寚瀹氬垵濮嬪寲鏃剁殑闃熷垪澶у皬, 浠ュ強enqueue,dequeue,isempty, isfull 鍥涚鏂规硶
銆€銆€浣跨敤鏂规硶濡備笅:銆€銆€銆€銆€
1 s = Queue(2) # 鍒濆鍖栦竴涓ぇ灏忎负 2鐨勯槦鍒?/span> 2 s.is_empty() # 鍒濆鍖栧悗, 闃熷垪涓虹┖, 杩斿洖 True 3 s.enqueue(1) # 灏?1鍔犲叆闃熷垪 4 s.enqueue(2) # 灏?2鍔犲叆闃熷垪 5 s.isfull() # 鍔犲叆浜嗕袱涓厓绱? 闃熷垪宸叉弧, 杩斿洖 True 6 s.dequeue() # 绉婚櫎涓€涓厓绱? 杩斿洖 1 7 s.dequeue() # 绉婚櫎涓€涓厓绱? 杩斿洖 2 8 s.is_empty() # 闃熷垪宸茬粡涓虹┖, 杩斿洖 True
class Queue(object): def __init__(self, size): self.__size = size self.__queue = [] def enqueue(self, x): if len(self.__queue) < self.__size: self.__queue.insert(0,x) else: return 鈥?/span>The queue is full!!!鈥?/span> def dequeue(self): return None if not len(self.__queue) else self.__queue.pop() def is_empty(self): return not len(self.__queue) def isfull(self): return len(self.__queue) == self.__size s = Queue(2) print(s.is_empty()) s.enqueue(1) s.enqueue(2) print(s.isfull()) print(s.dequeue()) print(s.dequeue()) print(s.is_empty())