# 在函數中定義函數
# 在函數中定義函數
剛才那些就是函數的基本知識了。我們來讓你的知識更進一步。在Python中我們可以在一個函數中定義另一個函數:
~~~
def hi(name="yasoob"):
print("now you are inside the hi() function")
def greet():
return "now you are in the greet() function"
def welcome():
return "now you are in the welcome() function"
print(greet())
print(welcome())
print("now you are back in the hi() function")
hi()
#output:now you are inside the hi() function
# now you are in the greet() function
# now you are in the welcome() function
# now you are back in the hi() function
# 上面展示了無論何時你調用hi(), greet()和welcome()將會同時被調用。
# 然后greet()和welcome()函數在hi()函數之外是不能訪問的,比如:
greet()
#outputs: NameError: name 'greet' is not defined
~~~
那現在我們知道了可以在函數中定義另外的函數。也就是說:我們可以創建嵌套的函數。現在你需要再多學一點,就是函數也能返回函數。
- 簡介
- 序
- 譯后感
- 原作者前言
- *args 和 **kwargs
- *args 的用法
- **kwargs 的用法
- 使用 *args 和 **kwargs 來調用函數
- 啥時候使用它們
- 調試 Debugging
- 生成器 Generators
- 可迭代對象(Iterable)
- 迭代器(Iterator)
- 迭代(Iteration)
- 生成器(Generators)
- Map和Filter
- Map
- Filter
- set 數據結構
- 三元運算符
- 裝飾器
- 一切皆對象
- 在函數中定義函數
- 從函數中返回函數
- 將函數作為參數傳給另一個函數
- 你的第一個裝飾器
- 使用場景
- 授權
- 日志
- 帶參數的裝飾器
- 在函數中嵌入裝飾器
- 裝飾器類
- Global和Return
- 多個return值
- 對象變動 Mutation
- slots魔法
- 虛擬環境
- 容器 Collections
- 枚舉 Enumerate
- 對象自省
- dir
- type和id
- inspect模塊
- 推導式 Comprehension
- 列表推導式
- 字典推導式
- 集合推導式
- 異常
- 處理多個異常
- finally從句
- try/else從句
- lambda表達式
- 一行式
- For - Else
- else語句
- open函數
- 目標Python2+3
- 協程
- 函數緩存
- Python 3.2+
- Python 2+
- 上下文管理器
- 基于類的實現
- 處理異常
- 基于生成器的實現