Python 浣跨敤def 寮濮嬪嚱鏁板畾涔夛紝绱ф帴鐫鏄嚱鏁板悕锛屾嫭鍙峰唴閮ㄤ负鍑芥暟鐨勫弬鏁帮紝鍐呴儴涓哄嚱鏁扮殑 鍏蜂綋鍔熻兘瀹炵幇浠g爜锛屽鏋滄兂瑕佸嚱鏁版湁杩斿洖鍊, 鍦 expressions 涓殑閫昏緫浠g爜涓敤 return 杩斿洖銆
1 2 |
def function_name(parameters): expressions |
瀹炰緥
1 2 3 4 |
def function(): print('This is a function') a = 1+2 print(a) |
涓婇潰鎴戜滑瀹氫箟浜嗕竴涓悕瀛椾负 function 鐨勫嚱鏁帮紝鍑芥暟娌℃湁涓嶆帴鍙楀弬鏁帮紝鎵浠ユ嫭鍙峰唴閮ㄤ负绌猴紝绱ф帴鐫灏辨槸 鍑芥暟鐨勫姛鑳戒唬鐮併傚鏋滄墽琛岃鑴氭湰锛屽彂鐜板苟娌℃湁杈撳嚭浠讳綍杈撳嚭锛屽洜涓烘垜浠彧瀹氫箟浜嗗嚱鏁帮紝鑰屽苟娌℃湁鎵ц鍑芥暟銆 杩欐椂鎴戜滑鍦 Python 鍛戒护鎻愮ず绗︿腑杈撳叆鍑芥暟璋冪敤 function(), 娉ㄦ剰杩欓噷璋冪敤鍑芥暟鐨勬嫭鍙蜂笉鑳界渷鐣ャ
閭d箞鍑芥暟鍐呴儴鐨勫姛鑳戒唬鐮佸皢浼氭墽琛岋紝杈撳嚭缁撴灉锛
1 2 |
This is a function 3 |
鏇村Python鐩稿叧鎶鏈枃绔狅紝璇疯闂甈ython鏁欑▼鏍忕洰杩涜瀛︿範锛
浠ヤ笂灏辨槸python閲宒ef鍑芥暟瀹氫箟鏄粈涔堟剰鎬濈殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒