Python函數進階

來源:互聯網
上載者:User

標籤:pre   訪問   ret   python函數   img   bsp   str   分享圖片   code   

1、嵌套函數

函數體中包含一個完整的函數,也可以叫作子函數

----------------------------嵌套函數範圍問題---------------------------

範圍

內函數可以訪問外函數的範圍。

外部函數不能訪問內建函式的範圍。

#coding:gbkdef test(m,n):        def test1():        print(‘hehe‘)        return m+n        return test1()print(test(8,9))

 

2、匿名函數

#coding:gbkfunc_name = lambda x,y:x+yprint(func_name)print(func_name(1,2))

等價於

def add(x,y):
    return x+y

 

lambda冒號前邊是參數,後邊是返回結果
函數名()代表函數的執行
區別:
lambda返回的是一個函數名

正常函數返回的是結果

a = {‘LiLei‘: 94, ‘lily‘: 80, ‘lucy‘: 75, ‘HanMeimei‘: 90}a.items()dict_items([(‘LiLei‘, 94), (‘lily‘, 80), (‘lucy‘, 75), (‘HanMeimei‘, 90)])list(a.items())[(‘LiLei‘, 94), (‘lily‘, 80), (‘lucy‘, 75), (‘HanMeimei‘, 90)]sorted(a.items(),key=lambda x:x[1],reverse=True)[(‘LiLei‘, 94), (‘HanMeimei‘, 90), (‘lily‘, 80), (‘lucy‘, 75)]

 

Python函數進階

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.