日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python学习-类(global、nonlocal、继承、多态)

發(fā)布時(shí)間:2023/12/19 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python学习-类(global、nonlocal、继承、多态) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • global與nonlocal
  • 繼承和多態(tài)

global與nonlocal

1.函數(shù)內(nèi)部定義的為局部變量,其作用域是局部作用域,函數(shù)外無法調(diào)用的
2.函數(shù)外定義的為全局變量,其作用域是全局作用域,如果在函數(shù)內(nèi)想要進(jìn)行修改,需要使用global修飾變量
3.外層函數(shù)的變量,如果想要在內(nèi)層函數(shù)進(jìn)行修改,需要nonlocal
關(guān)于這部分,有一篇博文寫得很詳細(xì),通過看他的這篇博文,我已經(jīng)看懂了兩個(gè)修飾詞的作用。
python:函數(shù)作用域 global與nonlocal
大家感興趣的可以去看看。上一段我修改過的代碼:

span = '123'def scope_test():def do_local():spam = "local spam"span = 'local'def do_nonlocal():nonlocal spamglobal spanspam = "nonlocal spam"span = "global"def do_global():global spamglobal spanspan = 'global'spam = "global spam"spam = "test spam"do_local()print("After local assignment:", spam, span)do_global()print("After global assignment:", spam, span)do_nonlocal()print("After nonlocal assignment:", spam, span)scope_test() print("In global scope:", spam, span)

這是對(duì)網(wǎng)上的一段代碼進(jìn)行了改進(jìn),別人的那個(gè)示例按照順序,容易誤導(dǎo)別人。大家關(guān)注我這里do_global和do_nonlocal的調(diào)用順序。
運(yùn)行結(jié)果:

剛開始看別人的代碼,我很疑惑,為什么別人的運(yùn)行結(jié)果,調(diào)用了global,結(jié)果還是nonlocal,見下圖:

后面仔細(xì)理解才發(fā)現(xiàn),因?yàn)闆]有全局變量spam,所以執(zhí)行了global修飾的spam,并沒有什么作用。而是直接打印了spam,由于上一次執(zhí)行了nonlocal spam,所以spam的結(jié)果是nonlocal spam。
然后我的代碼里面,do_nonlocal也是用的global span,而不是用nonlocal span,是因?yàn)闆]有局部變量span,編譯會(huì)報(bào)錯(cuò)。

這里說明一個(gè)問題,global 修飾的變量可以不存在,編譯不會(huì)報(bào)錯(cuò),nonlocal修飾的變量必須要存在,否則編譯會(huì)報(bào)錯(cuò)。
下面用一段代碼驗(yàn)證:

str1 = '123'def test1():def test2():nonlocal str2str2 = 'str2_test2'print(str2)nonlocal str1str1 = 'str1_test2'print(str1)def test3():global str1str1 = 'str1_test3'print(str1)global str2str2 = 'str1_test3'print(str2)str2 = '456'test3()test2()

編譯報(bào)錯(cuò):

表示str1不能用nonlocal修飾。

str1 = '123'def test1():def test2():nonlocal str2str2 = 'str2_test2'print('str2 = ', str2)global str1str1 = 'str1_test2'print('str1 = ', str1)def test3():global str1str1 = 'str1_test3'print('str1 = ', str1)global str2str2 = 'str2_test3'print('str2 = ', str2)str2 = '456'test3()print("After global assignment:", str1, str2)test2()print("After nonlocal assignment:", str1, str2)test1()

這段代碼把test2里面的nonlocal str1改成了global str1,然后輸出也加了打印語句,運(yùn)行結(jié)果:

因?yàn)橄日{(diào)用test3,使用了global str2,并且str2 = ‘str2_test3’,但是在運(yùn)行以后,打印出來的str2任然是456,這就說明了,global修飾了str2,但是沒有全局str2,編譯不會(huì)報(bào)錯(cuò),運(yùn)行也不會(huì)報(bào)錯(cuò)。

再次調(diào)用test3,結(jié)果為:str2_test2,并沒有對(duì)str2的值有改變。

繼承和多態(tài)

劃重點(diǎn):
Python 中所有的方法實(shí)際上都是 virtual 方法。
這和其他語言不一樣。

class CA:def myPrint(self):print('這是CA')class CB(CA):def myPrint(self):CA.myPrint(self)print(' 這是CB')class CC(CA):def myPrint(self):CA.myPrint(self)print(' 這是CC')ca = CA() cb = CB() cc = CC()def runTest(ca):ca.myPrint()for item in [ca, cb, cc]:runTest(item)

運(yùn)行結(jié)果:

這里體現(xiàn)了子類中重載函數(shù)時(shí),可以先調(diào)用基類中的方法,然后再繼續(xù)實(shí)現(xiàn)自己的功能。

除了單重集成以外,還可以有多重繼承,原理和單重繼承一樣,這里就不再細(xì)說了。

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的python学习-类(global、nonlocal、继承、多态)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。