绗洓绔?闈㈠悜瀵硅薄
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绗洓绔?闈㈠悜瀵硅薄相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/__call' title='__call'>__call
*args 璇█ 绯荤粺 瑙勮寖 ems 鍩虹被1銆佺畝杩伴潰鍚戝璞$殑涓夊ぇ鐗规€с€?/h1>
銆€銆€缁ф壙锛氬鏋滃涓被涓湁鍏卞悓鐨勬柟娉曪紝灏卞彲浠ユ妸鍏卞悓鐨勬柟娉曞啓鍦ㄥ熀绫讳篃灏辨槸鐖剁被涓紝鍙互渚涘叾鎵€鏈夌殑瀛愮被涔熷氨鏄淳鐢熺被浣跨敤锛岃繖鏍风殑璇濆氨涓嶇敤璁茶繖涓柟娉曞啓寰堝閬嶄簡锛屼娇寰椾唬鐮佹洿鍔犵畝娲侊紝鎻愰珮浜嗕唬鐮佺殑缂栧啓鏁堢巼锛屽瓙绫诲彲浠ョ户鎵跨埗绫荤殑闈炵鏈夊睘鎬у拰鏂规硶銆?/p>
銆€銆€鍦ㄨ鍥俱€佺増鏈€佽璇併€佸垎椤典腑浣跨敤杩囥€?/p>
銆€銆€灏佽锛氭妸澶氫釜鐩稿叧鐨勬柟娉曘€佸睘鎬у皝瑁呭湪涓€涓被涓€?/p>
銆€銆€澶氭€侊細鏈夊绉嶈〃鐜板舰寮忋€?/p>
2銆佷粈涔堟槸楦瓙妯″瀷锛?/h1>
銆€銆€楦瓙妯″瀷锛氬鏋滀竴涓笢瑗跨殑鍙0鍍忛腑瀛愶紝璧拌矾濮垮娍鍍忛腑瀛愶紝娓告吵濮垮娍鍍忛腑瀛愶紝閭d箞灏卞彲浠ョ湅浣滆繖涓笢瑗挎槸楦瓙銆?/p>
銆€銆€鍦ㄩ潰鍚戝璞′腑锛屼竴涓被鐨勬湁鏁堝畾涔変笉鏄敱瀹冩棦鎴愮殑绫绘垨鑰呯壒瀹氱殑鍊熷彛鍐冲畾鐨勶紝鑰屾槸鐢卞綋鍓嶆柟娉曞拰灞炴€у喅瀹氱殑銆?/p>
3銆乻uper鐨勪綔鐢紵
銆€銆€super锛堬級鍑芥暟鏄敤鏉ヨ皟鐢ㄧ埗绫荤殑涓€涓柟娉曘€傚湪澶氱户鎵夸腑锛屼负浜嗛伩鍏嶅湪涓や釜鐖剁被鐢卞悓鏍风殑鏂规硶鏃惰皟鐢ㄥ嚭閿欑殑闂锛屽氨闇€瑕乻uper鏂规硶浜嗭紝鍗曠户鎵夸腑锛屽鏋滃瓙绫荤殑鏂规硶鍜岀埗绫婚噸鍚嶄簡锛屼篃闇€瑕乻uper锛堬級鏂规硶鐢╯uper涔熸槸涓€涓壇濂界殑涔犳儻锛?/p>
銆€銆€浼氬湪浠ュ悗閬垮厤寰堝鍑洪敊銆?/p>
4銆乵ro鏄粈涔堬紵
銆€銆€褰撲袱涓埗绫讳腑鍑虹幇閲嶅悕鏂规硶鐨勬椂鍊欙紝濡備綍澶勭悊浠栦滑鐨勭户鎵垮叧绯伙紝鍗矼RO锛坢ethod resolution order锛夋柟娉曡В鏋愰『搴忛棶棰樸€?/p>
5銆佷粈涔堟槸c3绠楁硶銆?/h1>
銆€銆€c3绠楁硶鏈€鏃╂槸鐢ㄤ簬lisp锛屽悗鏉ョ敤鍒皃ython涓В鍐虫繁搴︿紭鍏堟悳绱腑涓嶆弧瓒虫湰鍦颁紭鍏堢骇鍜屽崟璋冩€х殑闂銆?鐢ㄦ潵璁$畻绫荤殑MRO)
銆€銆€瀹炰緥锛?/p>
class A: pass class B(A): pass class C(A): pass class D(B, C): pass class E(C, A): pass class F(D, E): pass class M: pass class N(M): pass class P(E, A): pass class X: pass class Q(P,N,X): pass class G(Q, F): pass class H(G, F): pass
銆€銆€璁$畻H鐨刴ro锛?/p>
L(A) = A L(B) = B + L(A) + BA 銆€銆€BA L(C) = C + L(A) + A 銆€銆€CA L(D) = D + L(B) +L(C) + BC 銆€銆€DBCA L(E) = E + L(C) +L(A) + CA 銆€銆€ECA L(F) = F +L(D) + L(E) + DE 銆€銆€F + DBCA + ECA = FDBECA L(M) = M L(N) = N + L(M) + M 銆€銆€NM L(P) = P + L(E) +L(A) + EA 銆€銆€PECA L(X) = X L(Q) = Q + L(P) + L(N) + L(X) + PNX QPECANMX L(G) = G + L(Q) + L(F) + QF G + QPECANMX + FDBECA = GQPFDBECANMX L(H) = H + L(G) + L(F) + GF H + GQPFDBECANMX + FDBECA = HGQPFDBECANMX
6銆佸垪涓鹃潰鍚戝璞′腑甯﹀弻涓嬪垝绾跨殑鐗规畩鏂规硶銆?/h1>
銆€銆€__init__: 鏋勯€犲嚱鏁帮紝鍦ㄧ敓鎴愬璞℃椂璋冪敤銆?/p>
銆€銆€__del__: 鏋愭瀯鍑芥暟锛岄噴鏀惧璞℃椂浣跨敤銆?/p>
銆€銆€__repr__: 鎵撳嵃锛岃浆鎹€?/p>
銆€銆€__new__: 鏋勯€犵被鐨勫疄渚嬪寲銆?/p>
銆€銆€__setitem: 鎸夌収绱㈠紩璧嬪€笺€?/p>
銆€銆€__getitem__: 鎸夌収绱㈠紩鑾峰彇鍊笺€?/p>
銆€銆€__len__锛氳幏鍙栭暱搴︺€?/p>
銆€銆€__call__锛氳皟鐢ㄣ€?/p>
7銆佸弻涓嬪垝绾垮拰鍗曚笅鍒掔嚎鐨勫尯鍒€?/h1>
銆€銆€鍗曚笅鍒掔嚎锛氭柟娉曟垨鑰呭睘鎬у墠闈紝璁や负瀹冩椂璇ユ柟娉曟垨鑰呭睘鎬т负璇ョ被鐨勭鏈夊睘鎬ф垨鑰呮柟娉曘€?/p>
銆€銆€鍙屼笅鍒掔嚎锛歱ython璁捐姝ょ殑鐪熸鐩殑鏄负浜嗛伩鍏嶅瓧绫昏鐩栫埗绫荤殑鏂规硶銆?/p>
銆€銆€寮€澶寸粨灏惧弻涓嬪垝绾匡細涓€鑸潵璇確_this__杩欑寮€澶寸粨灏鹃兘鍔犲弻涓嬪垝绾跨殑鏂规硶琛ㄧず杩欐槸python鑷繁璋冪敤鐨勶紝浣犱笉瑕佽皟鐢ㄣ€?/p>
8銆佸疄渚嬪彉閲忓拰绫诲彉閲忕殑鍖哄埆銆?/h1>
銆€銆€绫诲彉閲忥細瀹氫箟鍦ㄧ被閲岄潰锛岄€氳繃绫诲悕鎴栧璞″悕寮曠敤锛屽鏋滄槸閫氳繃瀵硅薄鍚嶅紩鐢紝浼氬厛鎵炬湁娌℃湁杩欎釜鍚屽悕鐨勫疄渚嬪彉閲忥紝濡傛灉娌℃湁锛屽紩鐢ㄥ埌鐨勬墠鏄被鍙橀噺锛岀被鍙橀噺鐨勬洿鏂帮紝鍙兘閫氳繃绫诲悕锛屽舰濡?绫诲悕.a = 55 锛屼笉瑕佹寚鏈涢€氳繃瀹炰緥寮曠敤绫诲彉閲忔潵鏇?/p>
銆€銆€鏂扮被鍙橀噺銆?/p>
銆€銆€瀹炰緥鍙橀噺锛?瀹氫箟鍦ㄦ柟娉曢噷闈㈢殑鍙橀噺锛屼竴鑸湪__init__閲岄潰锛屽彧鑳介€氳繃瀵硅薄鍚嶅紩鐢紝瀹炰緥鍙橀噺鐨勫鍔犮€佹洿鏂板舰寮忥紝褰㈠self.a = 55 銆?/p>
銆€銆€鍖哄埆鍦ㄤ簬锛岀被鍙橀噺灞炰簬绫伙紝涓€涓柟娉曞皢鍏舵敼鍙橈紝鍏朵粬鏂规硶璋冪敤鐨勫氨鏄敼鍙樹箣鍚庣殑浜嗭紝瀹炰緥鍙橀噺鏄睘浜庢柟娉曠鏈夌殑锛屾柟娉曞皢鍏舵敼鍙樹箣鍚庯紝瀵瑰叾浠栨柟娉曟病鏈夊奖鍝嶃€?/p>
9銆侀潤鎬佹柟娉曞拰绫绘柟娉曠殑鍖哄埆銆?/h1>
銆€銆€绫绘柟娉曪細瀹氫箟锛氫娇鐢ㄨ楗板櫒@classmethod銆傜涓€涓弬鏁板繀椤绘槸褰撳墠绫诲璞★紝璇ュ弬鏁板悕涓€鑸害瀹氫负“cls”锛岄€氳繃瀹冩潵浼犻€掔被鐨勫睘鎬у拰鏂规硶锛堜笉鑳戒紶瀹炰緥鐨勫睘鎬у拰鏂规硶锛夛紱
銆€銆€闈欐€佹柟娉曪細瀹氫箟锛氫娇鐢ㄨ楗板櫒@staticmethod銆傚弬鏁伴殢鎰忥紝娌℃湁“self”鍜?ldquo;cls”鍙傛暟锛屼絾鏄柟娉曚綋涓笉鑳戒娇鐢ㄧ被鎴栧疄渚嬬殑浠讳綍灞炴€у拰鏂规硶锛涜皟鐢細瀹炰緥瀵硅薄鍜岀被瀵硅薄閮藉彲浠ヨ皟鐢ㄣ€?/p>
10銆乮sinstance 鍜?type 鐨勪綔鐢ㄣ€?/h1>
銆€銆€isinstance: 鍒ゆ柇涓€涓璞℃槸鏌愪釜绫绘垨瀛愮被鐨勫疄渚嬨€?isinstance(object,type-or-tuple-or-class) -> bool)
銆€銆€type: 寰楀埌浼犲叆鐨刼bject鐨勭被鍨嬨€?type(object)--绫诲瀷)
11銆佹湁鐢ㄨ繃with statement鍚楋紵瀹冪殑濂藉鏄粈涔堛€?/h1>
銆€銆€鐢ㄨ繃锛屾墦鏉ユ枃浠剁殑鏃跺€欙紝with鐨勪綔鐢ㄦ槸鍦ㄤ唬鐮佹墽琛岀粨鏉熷悗锛岃嚜鍔ㄥ叧闂枃浠讹紝涓嶈绋嬪簭鏄互浣曠鏂瑰紡缁撴潫鐨勶紝濡傛灉鎵ц浠g爜杩囩▼涓彂鐢熷紓甯革紝with浼氬湪澶栭儴寮傚父鍓嶅叧闂枃浠躲€?/p>
12銆佷笅鍒楁暟鎹摢浜涙槸涓嶅彲杩唬鐨勶紵
銆€銆€object涓嶅彲杩唬
13銆佸疄鐜颁竴涓猻ingleton鍗曚緥绫伙紝瑕佹眰閬靛惊鍩烘湰璇█缂栫▼瑙勮寖銆?/h1>
銆€銆€鍗曚緥妯″紡锛圫ingleton Pattern锛夋槸涓€绉嶅父鐢ㄧ殑杞欢璁捐妯″紡锛岃妯″紡鐨勪富瑕佺洰鐨勬槸纭繚鏌愪竴涓被鍙湁涓€涓疄渚嬪瓨鍦ㄣ€傚綋浣犲笇鏈涘湪鏁翠釜绯荤粺涓紝鏌愪釜绫诲彧鑳藉嚭鐜颁竴涓疄渚嬫椂锛屽崟渚嬪璞″氨鑳芥淳涓婄敤鍦恒€?/p>
def Singleton(cls): _instance = {} def _singleton(*args, **kargs): if cls not in _instance: _instance[cls] = cls(*args, **kargs) return _instance[cls] return _singleton @Singleton class A(object): a = 1 def __init__(self, x=0): self.x = x a1 = A(2) a2 = A(3)
14銆佽鎻忚堪with鐨勭敤娉曪紝濡傛灉鑷繁鐨勭被闇€瑕佹敮鎸亀ith 璇彞锛岄渶瑕佹€庝箞涔﹀啓锛?/h1>
銆€銆€with 鍚庨潰鐨勮鍙ヨ姹傚€煎悗杩斿洖瀵硅薄鐨勨€榑_enter__()鈥樻柟娉曡璋冪敤锛岃繖涓柟娉曠殑杩斿洖鍊煎皢浼氳繑鍥炵粰as鍚庨潰鐨勫彉閲忋€?/p>
銆€銆€褰搘ith鍚庨潰鐨勪唬鐮佸叏閮ㄦ墽琛屽畬鍚庡皢璋冪敤鍓嶉潰杩斿洖瀵硅薄鐨勨€榑_exit__()鈥樻柟娉曘€?/p>
銆€銆€鍙渶瑕佸湪绫婚噷鍐欏叆__enter__()鏂规硶鍜宊_exit__()鏂规硶鍗冲彲浣跨敤with.
15銆佸浣曞垽鏂竴涓璞℃槸鍙皟鐢ㄥ璞★紵
銆€銆€1銆佷娇鐢ㄥ唴缃嚱鏁癱allable鍑芥暟锛氬彲浠ユ娴嬪璞℃槸鍚﹀彲浠ヨ皟鐢紝杩斿洖True锛屼笉涓€瀹氬彲浠ヨ皟鐢紝浣嗘槸杩斿洖False锛屼竴瀹氫笉鍙互璋冪敤銆?/p>
銆€銆€2銆佸垽鏂璞℃槸鍚︽湁__call__鏂规硶銆?/p>
銆€銆€3銆佸垽鏂被鍨嬫槸鍚︽槸FunctionTypetype(func) is FunctionType 鎴栬€?isinstance(func, FunctionType)
銆€銆€鑳藉疄鐜扮被涓璤_call__锛堬級鏂规硶鐨勫氨鏄彲璋冪敤绫汇€?/p>
16銆佽瀹炵幇涓€涓爤
class zhan(object): def __init__(self): self.items = [] def panduan(self): return self.items == [] def size(self): return len(self.items) def peek(self): return self.items[len(self.items) - 1] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() obj = zhan() obj.push(鈥?/span>h鈥?/span>) obj.push(鈥?/span>a鈥?/span>) obj.push(鈥?/span>s鈥?/span>) print(obj.size()) print(obj.peek()) print(obj.pop()) print(obj.peek()) print(obj.size()) print(obj.pop()) print(obj.size()) print(obj.panduan())
17銆佸叧浜巔ython 绫荤殑缁ф壙涓嶆纭殑璇存硶鏄紵
銆€銆€python鐨勭被鏃犳硶缁ф壙锛屽彧鑳芥湁涓€涓埗绫伙紝鍙互缁ф壙锛屼絾鏄笉鑳界户鎵跨埗绫荤殑鏋勯€犲嚱鏁般€傦紙浠ヤ笂璇存硶鏄笉姝g‘鐨勶級
18銆?-----
19銆佽鐢ㄤ袱涓槦鍒楀疄鐜颁竴涓爤锛堢粰鍑轰负浠g爜鍗冲彲锛?/h1>
class zhan1(object):
def __init__(self):
self.l1 = []
self.l2 = []
def add(self, a):
if len(self.l1) == 0:
self.l2.append(a)
if len(self.l2) == 0:
self.l1.append(a)
def pop(self, ):
if len(self.l1) == 0:
for i in self.l2[:-1]:
self.l1.append(i)
self.l2.clear()
elif len(self.l2) == 0:
for i in self.l1[:-1]:
self.l2.append(i)
self.l1.clear()
else:
print(鈥?/span>鏍堝凡缁忎负绌?/span>鈥?/span>)
def check(self):
if len(self.l1) == 0:
print(self.l2)
elif len(self.l2) == 0:
print(self.l1)
obj = zhan1()
obj.add(鈥?/span>a鈥?/span>)
obj.add(鈥?/span>b鈥?/span>)
obj.add(鈥?/span>c鈥?/span>)
obj.check()
obj.pop()
obj.check()
obj.pop()
obj.check()
obj.add(鈥?/span>d鈥?/span>)
obj.check()
obj.pop()
obj.check()
class zhan1(object): def __init__(self): self.l1 = [] self.l2 = [] def add(self, a): if len(self.l1) == 0: self.l2.append(a) if len(self.l2) == 0: self.l1.append(a) def pop(self, ): if len(self.l1) == 0: for i in self.l2[:-1]: self.l1.append(i) self.l2.clear() elif len(self.l2) == 0: for i in self.l1[:-1]: self.l2.append(i) self.l1.clear() else: print(鈥?/span>鏍堝凡缁忎负绌?/span>鈥?/span>) def check(self): if len(self.l1) == 0: print(self.l2) elif len(self.l2) == 0: print(self.l1) obj = zhan1() obj.add(鈥?/span>a鈥?/span>) obj.add(鈥?/span>b鈥?/span>) obj.add(鈥?/span>c鈥?/span>) obj.check() obj.pop() obj.check() obj.pop() obj.check() obj.add(鈥?/span>d鈥?/span>) obj.check() obj.pop() obj.check()
銆€銆€缁撴灉锛?/p>
[鈥?/span>a鈥?/span>, 鈥?/span>b鈥?/span>, 鈥?/span>c鈥?/span>]
[鈥?/span>a鈥?/span>, 鈥?/span>b鈥?/span>]
[鈥?/span>a鈥?/span>]
[鈥?/span>a鈥?/span>, 鈥?/span>d鈥?/span>]
[鈥?/span>a鈥?/span>]
20銆佸凡鐭ュ涓嬮摼琛ㄧ被锛岃瀹炵幇鍗曢摼琛ㄩ€嗙疆銆?/h1>
class Node:
def __init__(self, value=None, next=None):
self.value = value
self.next = next
def nizhuan(link):
pre = link
cur = link.next
pre.next = None
while cur:
tem = cur.next
cur.next = pre
pre = cur
cur = tem
return pre
if __name__ == 鈥?/span>__main__鈥?/span>:
link = Node(1, Node(2, Node(3, Node(4, Node(5, Node(6, Node(7, Node(8, Node(9)))))))))
root = nizhuan(link)
while root:
print(root.value)
root = root.next
21銆佺被鐨勫姞杞介『搴?/h1>
class Node: def __init__(self, value=None, next=None): self.value = value self.next = next def nizhuan(link): pre = link cur = link.next pre.next = None while cur: tem = cur.next cur.next = pre pre = cur cur = tem return pre if __name__ == 鈥?/span>__main__鈥?/span>: link = Node(1, Node(2, Node(3, Node(4, Node(5, Node(6, Node(7, Node(8, Node(9))))))))) root = nizhuan(link) while root: print(root.value) root = root.next
銆€銆€鏋勯€?gt;闈欐€?gt;鐖剁被
22銆佸弬鑰冧笅闈唬鐮佺墖娈?/h1>
class Context:
pass
with Context() as ctx:
ctx.do_something
class Context: pass with Context() as ctx: ctx.do_something
銆€銆€缁撴灉锛?/p>
class Context(object):
def __enter__(self):
print(鈥榚nter鈥?
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print()
def do_something(self):
print(鈥榓鈥?
with Context() as ctx:
ctx.do_something()
23銆佷笅闈唬鐮佽緭鍑烘椂浠€涔堬紵璇风粰鍑虹瓟妗堝苟瑙i噴銆?/h1>
class Parent(object):
x = 1
class Child1(Parent):
pass
class Child2(Parent):
pass
print(Parent.x)
print(Child1.x)
print(Child2.x)
Child1.x = 2
print(Parent.x)
print(Child1.x)
print(Child2.x)
Parent.x = 2
print(Parent.x)
print(Child1.x)
print(Child2.x)
class Parent(object): x = 1 class Child1(Parent): pass class Child2(Parent): pass print(Parent.x) print(Child1.x) print(Child2.x) Child1.x = 2 print(Parent.x) print(Child1.x) print(Child2.x) Parent.x = 2 print(Parent.x) print(Child1.x) print(Child2.x)
銆€銆€缁撴灉锛?/p>
1 1 1 1 2 1 2 2 2
銆€銆€瑙i噴锛氱埗绫讳腑鐨勫彉閲忕被浼间簬鍏ㄥ眬鍙橀噺锛屽彧鑳藉湪鍏ㄥ眬浣滅敤鍩熼噷淇敼锛屽瓙绫讳腑鐨勫睘浜庡眬閮ㄥ彉閲忥紝灞€閮ㄥ彉閲忎腑瀵瑰叏灞€鍙橀噺杩涜淇敼鍙兘浣滅敤浜庤嚜宸辩被涓紝鏃犳硶鏀瑰彉鍘讳粬浣滅敤鍩熺殑璇ュ彉閲忋€?/p>
24銆?/h1>
25銆佽缁欏嚭涓嬮潰浠g爜鐗囨鐨勮緭鍑猴紝璇风畝杩颁笂闈唬鐮侀渶瑕佹敼杩涚殑鍦版柟銆?/h1>
class singlenton:
_instance = None
def __new__(cls, *args, **kwargs):
print(鈥?/span>new鈥?/span>)
if cls._instance is None:
print(鈥?/span>Create鈥?/span>)
cls._instance = super().__new__(cls, *args, **kwargs)
return cls._instance
def __init__(self):
print(鈥?/span>initalize鈥?/span>)
self.prop = None
s1 = singlenton()
s2 = singlenton()
class singlenton: _instance = None def __new__(cls, *args, **kwargs): print(鈥?/span>new鈥?/span>) if cls._instance is None: print(鈥?/span>Create鈥?/span>) cls._instance = super().__new__(cls, *args, **kwargs) return cls._instance def __init__(self): print(鈥?/span>initalize鈥?/span>) self.prop = None s1 = singlenton() s2 = singlenton()
銆€銆€缁撴灉锛?/p>
new
Create
initalize
new
initalize
銆€銆€鏀硅繘锛氬簲璇ユ妸__init__鍑芥暟鏀惧湪鏈€涓婇潰锛?/p>
26銆佽绠€鍗曡В閲妏ython涓殑闈欐€佹柟娉曞拰绫绘柟娉曪紝骞跺皢浠ヤ笅浠g爜濉啓瀹屾暣銆?/h1>
銆€銆€绫绘柟娉曪細浣跨敤瑁呴グ鍣ˊclassmethod銆傜涓€涓弬鏁板繀椤绘槸褰撳墠绫诲璞★紝璇ュ弬鏁板悕涓€鑸害瀹氫负“cls”锛岄€氳繃瀹冩潵浼犻€掔被鐨勫睘鎬у拰鏂规硶锛堜笉鑳戒紶瀹炰緥鐨勫睘鎬у拰鏂规硶锛?/p>
銆€銆€闈欐€佹柟娉曪細浣跨敤瑁呴グ鍣ˊstaticmethod銆傚弬鏁伴殢鎰忥紝娌℃湁“self”鍜?ldquo;cls”鍙傛暟锛屼絾鏄柟娉曚綋涓笉鑳戒娇鐢ㄧ被鎴栧疄渚嬬殑浠讳綍灞炴€у拰鏂规硶锛涜皟鐢細瀹炰緥瀵硅薄鍜岀被瀵硅薄閮藉彲浠ヨ皟鐢ㄣ€?/p>
class A(object): def foo(self,x) print ‘executing foo(%s, %s)‘%(self,x) @classmethod def class_foo(cls,x): print ‘executing class_foo(%s, %s)‘%(cls,x) @staticmethod def static_foo(x): print ‘executing static_foo(%s)‘%(x) a= A() # 璋冪敤 foo鍑芥暟,鍙傛暟浼犲叆 1 a.foo(1) # 璋冪敤 class_foo鍑芥暟,鍙傛暟浼犲叆 1 A.class_foo(1) # 璋冪敤 static_foo鍑芥暟,鍙傛暟浼犲叆 1 A.static_foo(1)銆€/ a.ststic_foo(1)
27銆?/h1>
28銆佺紪绋嬪疄鐜颁竴涓厛杩涘厛鍑虹殑闃熷垪绫伙紝鑳借拷瀹氬垵璇嗗寲鏃剁殑闃熷垪澶у皬銆?/h1>
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())
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())
銆€銆€
銆€銆€
銆€銆€
銆€銆€
銆€銆€
銆€銆€
以上是关于绗洓绔?闈㈠悜瀵硅薄的主要内容,如果未能解决你的问题,请参考以下文章