python 面向對象,python函數作用域_Python命名空間和作用域的基本認識和一點小魔法

 2023-10-24 阅读 25 评论 0

摘要:作為初學者,簡單總結自己對Python命名空間(namespace)和作用域(scope)的認識。 Python在名稱空間搜尋變量和函數的順序可以認為是: 函數內部 -> (嵌套)父函數內部 -> 模塊內部 -> ?built_in內置模塊 1. 函數內部(local) 簡單舉例說明: python 面向對象

作為初學者,簡單總結自己對Python命名空間(namespace)和作用域(scope)的認識。

Python在名稱空間搜尋變量和函數的順序可以認為是:

函數內部 -> (嵌套)父函數內部 -> 模塊內部 -> ?built_in內置模塊

1. 函數內部(local)

簡單舉例說明:

python 面向對象?x = 123

def func():

x = 456

print 'inner x =', x

func()

print 'outer x =', x

python 全局變量?1

2

3

4

5

6

python迭代器。7

8

x=123

deffunc():

x=456

print'inner x =',x

python作用域和參數、func()

print'outer x =',x

輸出:

inner x = 456

outer x = 123

1

查看變量類型的python內置函數,2

innerx=456

outerx=123

模塊級變量x的值是123,而在函數func內部,local變量x的值是456。可以看到,函數是沒有改變外部x的值的。

2. 父函數內部

x = 123

python數組。def func():

x = 456

print 'inner x =', x

def child_func():

print 'x from parent function =', x

child_func()

python變量作用域,func()

print 'outer x =', x

1

2

3

4

python作用域和命名空間、5

6

7

8

9

10

python命名空間更改?11

12

13

x=123

deffunc():

x=456

python中的命名空間指什么。print'inner x =',x

defchild_func():

print'x from parent function =',x

child_func()

func()

print'outer x =',x

python作用域規則。輸出為:

inner x = 456

x from parent function = 456

outer x = 123

1

2

python內置函數大全?3

innerx=456

xfromparentfunction=456

outerx=123

child_func是func的嵌套函數,它的x值,來自其父函數func內部,而非模塊級變量。

3.模塊內部

lambda函數 python。模塊內也就是我們常說的全局變量。

x = 123

def func():

print 'inner x =', x

func()

print 'outer x =', x

python中[::-1],1

2

3

4

5

6

python里的各種范圍?7

x=123

deffunc():

print'inner x =',x

func()

print'outer x =',x

python中變量的作用域,輸出為:

inner x = 123

outer x = 123

1

2

innerx=123

python中內置函數有哪些?outerx=123

func內部沒有命名變量x,print語句使用的x來自函數外的模塊級變量x。

4. Built_in內置模塊

print abs(-123)

def abs(x):

return x + 100

print abs(-123)

1

2

3

4

5

6

printabs(-123)

defabs(x):

returnx+100

printabs(-123)

輸出:

123

-23

1

2

123

-23

這里第一次調用的abs函數來自__builtins__。第二次調用來自模塊內部。

5. 神奇的global關鍵字

global用于向函數中引入全局變量,這個關鍵字是非常神奇的。

x = 123

def func():

x = 333

if False:

global x

print 'inner x = ', x

func()

print 'outer x = ', x

1

2

3

4

5

6

7

8

9

10

x=123

deffunc():

x=333

ifFalse:

globalx

print'inner x = ',x

func()

print'outer x = ',x

按照一般的理解,global x聲明位于x變量使用之后,并且,它永遠不可能得到執行,那么前面x = 333使用的是local變量才是。但最終的運行結果很出乎意料:

SyntaxWarning: name 'x' is assigned to before

global declaration

global x

inner x = 333

outer x = 333

1

2

3

4

5

SyntaxWarning:name'x'isassignedtobefore

globaldeclaration

globalx

innerx=333

outerx=333

Python會給出一個警告,但打印出的結果卻顯示,函數中使用了模塊級變量(全局變量)x。這說明,只要有global語句在源代碼中,不管這條語句是否執行,也不管你把這句代碼放在函數的哪個位置,python解釋器都將使用全局變量。文檔中提到:

the language definition is evolving towards static name resolution, at “compile” time, so don’t rely on dynamic name resolution!

6. 被調用函數無法共享調用函數的命名空間

def caller():

x = 123

callee()

def callee():

print 'callee inner x', x

caller()

1

2

3

4

5

6

7

8

defcaller():

x=123

callee()

defcallee():

print'callee inner x',x

caller()

執行時將出現NameError: global name ‘x’ is not defined

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/163846.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息