激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python基礎(chǔ)入門之魔法方法與異常處理

Python基礎(chǔ)入門之魔法方法與異常處理

2022-03-04 17:40嚶嚶906 Python

在python中,所有以“__"雙下劃線包起來的方法,都統(tǒng)稱為魔法方法,下面這篇文章主要給大家介紹了關(guān)于Python基礎(chǔ)入門之魔法方法與異常處理的相關(guān)資料,需要的朋友可以參考下

一.魔法方法

1.屬性訪問

通??梢酝ㄟ^點(diǎn)(.)操作符的形式去訪問對(duì)象的屬性。

?
1
2
3
4
5
6
7
8
9
10
class c:
    def __init__(self):
        self.x='x-man'
c=c()
c.x
'x-man'
getattr(c , 'x' , '木有這個(gè)屬性')
'x-man'
getattr(c , 'y' , '木有這個(gè)屬性')
'木有這個(gè)屬性'

魔法方法:

(1)定義當(dāng)用戶試圖獲取一個(gè)不存在的屬性時(shí)的行為。

?
1
__getattr__(self,name) 

(2)定義當(dāng)該類的屬性被訪問時(shí)的行為。

?
1
__getattribute__(self,name)

(3)定義當(dāng)一個(gè)屬性被設(shè)置時(shí)的行為。

?
1
__setattr__(self,name,value)

(4)定義當(dāng)一個(gè)屬性被刪除時(shí)的行為。

?
1
__delattr__(self,name)

2.描述符

(1)用于訪問屬性,它返回屬性的值。

?
1
__get__(self,instance,owner)

(2)將在屬性分配操作中調(diào)用,不返回任何內(nèi)容。

?
1
__set__(self,instance,value)

(3)控制刪除操作,不返回任何內(nèi)容。

?
1
__delete__(self,instance)
?
1
2
3
4
5
6
7
8
9
class mydescriptor:
    def __get__(self,instance,owner):
        print("getting...",self,instance,owner)
    def __set__(self,instance,value):
        print("setting...",self,instance,value)
    def __delete__(self,instance):
        print("deleting...",self,instance)
class test:
    x =mydescriptor()

3.定制序列

魔法方法:

?
1
2
3
4
5
6
7
__len__(self)
__getitem__(self,key)
__setitem__(self,key,value)
__delitem__(self,key)
__iter__(self)
__reversed__(self)
__contains__(self,item)

4.迭代器

?
1
2
3
4
5
6
7
for i in "fishc":
    print(i)
f
i
s
h
c

字符串就是一個(gè)容器,同時(shí)也是一個(gè)迭代對(duì)象,for語句的作用就是觸發(fā)其迭代功能,每次從容器里依次拿出一個(gè)數(shù)據(jù),這就是迭代操作。

python提供了兩個(gè)bif:iter()和next()。

對(duì)一個(gè)可迭代對(duì)象調(diào)用iter()就得到它的迭代器,調(diào)用next()迭代器就會(huì)返回下一個(gè)值。

?
1
2
3
4
5
6
7
8
9
10
11
12
string="fishc"
it=iter(string)
next(it)
'f'
next(it)
'i'
next(it)
's'
next(it)
'h'
next(it)
'c'

5.生成器

對(duì)于調(diào)用一個(gè)普通的python函數(shù),一般是從函數(shù)的第一行代碼開始執(zhí)行,結(jié)束于return語句、異?;蛘咚姓Z句執(zhí)行完畢。

二.異常處理

1.異常類型

(1)assertionerror:斷言語句(assert)失敗。

?
1
2
3
4
5
6
7
8
9
my_list=["小甲魚"]
assert len(my_list)>0
my_list.pop()
'小甲魚'
assert len(my_list)>0
traceback (most recent call last):
  file "<pyshell#3>", line 1, in <module>
    assert len(my_list)>0
assertionerror

(2)attributeerror:嘗試訪問未知的對(duì)象屬性。

?
1
2
3
4
5
6
my_list=[]
my_list.fishc
traceback (most recent call last):
  file "<pyshell#1>", line 1, in <module>
    my_list.fishc
attributeerror: 'list' object has no attribute 'fishc'

(3)indexerror:索引超出序列的范圍。

?
1
2
3
4
5
6
my_list=[1,2,3]
my_list[3]
traceback (most recent call last):
  file "<pyshell#3>", line 1, in <module>
    my_list[3]
indexerror: list index out of range

(4)keyerror:字典中查找一個(gè)不存在的關(guān)鍵字。

?
1
2
3
4
5
6
my_dict={"one":1,"two":2}
my_dict["three"]
traceback (most recent call last):
  file "<pyshell#5>", line 1, in <module>
    my_dict["three"]
keyerror: 'three'

(5)nameerror:嘗試訪問一個(gè)不存在的變量。

?
1
2
3
4
5
fishc
traceback (most recent call last):
  file "<pyshell#6>", line 1, in <module>
    fishc
nameerror: name 'fishc' is not defined

(6)oserror:操作系統(tǒng)產(chǎn)生的異常。

(7)syntaxerror:python的語法錯(cuò)誤。

?
1
2
print"i love fishc.com"
syntaxerror: missing parentheses in call to 'print'. did you mean print(...)?

(8)typeerror:不同類型間的無效操作。

?
1
2
3
4
5
1+'1'
traceback (most recent call last):
  file "<pyshell#8>", line 1, in <module>
    1+'1'
typeerror: unsupported operand type(s) for +: 'int' and 'str'

(9)zerodivisionerror:除數(shù)為零。

?
1
2
3
4
5
5/0
traceback (most recent call last):
  file "<pyshell#9>", line 1, in <module>
    5/0
zerodivisionerror: division by zero

2.try-except 語句

?
1
2
3
4
5
6
7
8
try:
    int('abc')
    sum =1+'1'
    f =open('我是一個(gè)不存在的文檔.txt')
    print(f.read())
    f.close()
except (valueerror,typeerror,oserror) as reason:
    print('出錯(cuò)\n錯(cuò)誤原因是:'+str(reason)')

3.try-finally 語句

?
1
2
3
4
5
6
7
8
try:
    f =open('我是一個(gè)不存在的文檔.txt')
print(f.read())
    sum=1+'1'
except:
    print('出錯(cuò)啦')
finally:
    f.close()

4.raise 語句

?
1
2
3
4
5
raise zerodivisionerror5/0
traceback (most recent call last):
  file "<pyshell#9>", line 1, in <module>
    5/0
zerodivisionerror: division by zero

5.豐富的else語句

?
1
2
3
4
5
6
7
8
9
10
11
12
13
try:
    int('abc')
except valueerror as reason:
    print('出錯(cuò)啦:'+str(reason))
else:
    print('沒有任何異常!')
 
try:
    with open('data.txt','w') as f:
        for each_line in f:
            print(each_line)
except oserror as reason:
    print('出錯(cuò)啦:'+str(reason))

總結(jié)

到此這篇關(guān)于python基礎(chǔ)入門之魔法方法與異常處理的文章就介紹到這了,更多相關(guān)python魔法方法與異常處理內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/weixin_63414347/article/details/121442319

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产91久久久 | av在线播放亚洲 | 性猛交ⅹxxx乱巴西 欧美日韩1区2区3区 | 久久久成人一区二区免费影院 | 欧美一级免费看 | 日本一区二区不卡高清 | 久久久久久精 | 亚洲精品a在线观看 | 免费国产精品视频 | 国产精品自在线拍 | 亚洲射逼 | 激情宗合 | 99在线在线视频免费视频观看 | 国产一区二区三区四 | 精品一区二区三区在线观看视频 | 中国大陆高清aⅴ毛片 | 亚洲一区在线不卡 | 污污短视频 | 在线播放av网址 | 日韩精品中文字幕一区二区 | 国产成人精品一区在线播放 | 婷婷中文字幕一区二区三区 | 黄网站在线免费 | 中文字幕 欧美 日韩 | 视频一区二区久久 | 暴力肉体进入hdxxxx0 | 亚洲综合视频网 | 91精品影视 | 网站一区 | 亚洲一区在线视频观看 | 国产精品99久久久久久大便 | 久久久久亚洲精品国产 | 午夜国产精品成人 | 美国一级黄色毛片 | 九色成人在线 | 国产1区2区3区中文字幕 | av电影在线观看网站 | 天天干干 | 国产日韩久久久久69影院 | 美女性感毛片 | 国产精品视频海角社区88 |