Python基础-异常/单例模式

IndexError:

a=[1,2]
try:
    print(a[3])
except IndexError as e:
    print('出现错误了',e)
print('程序到这了')

AttributeError:

class A():
    pass
try:
    print(A.x)
except AttributeError as x:
    print(x)

KeyError

a={'x':1,'y':2,'z':3}
try:
    print(a['g'])
except KeyError as f:
    print(f)

StopIteration:

a=[1,2,3,4,5]
b=iter(a)
while True:
    try:
        print(next(b))
    except StopIteration as f:
        print('错了!',f)
        break

只返回第一个接收到的错误:

a=[1, ]
try:
    print(a[2]) #只返回第一个接收到的错误
    print(1/0)
except(ZeroDivisionError,IndexError) as e:
    print(e)

a=[1, ]
try:
    print(1/0)
    print(a[2])
except(ZeroDivisionError,IndexError) as e:
    print(e)

a=[1, ]
try:
    print(33333333)
except(ZeroDivisionError,IndexError) as e:
    print(e)
else:
    print('meicuo')

可以接收所有类型的错误,但是只返回接收到的第一个错误

a=[1, ]
try:
    print(a[2])
    print('x')
    print(2/0)
    print('y')
except:
    print(2)   #可以接收所有类型的错误,但是只返回接收到的第一个错误
else:
    print(3)

try,else,finally:

a=[1, ]

try:
    print('9')
except:
    print(2)   #可以接收所有类型的错误,但是只返回接收到的第一个错误
else:
    print(3)
finally:
    print('123')  #不管程序异常与否都执行

try:
    f=open('b.txt','w')
    f.write('lll')
except:
    print('chusihla')
finally:
    print('lailelaodi')
    f.close()

用with,open打开文件

with open('b.txt','r') as f:
    x=f.read()
    print(x)

捕捉主动抛出的异常

a=10
try:
    raise IndexError('cuole') #主动抛出异常
    print('123')
except IndexError as f:
    print(f)

例题:

class MyError(Exception):
    def __init__(self,msg):
        self.msg=msg
    def __str__(self):
        return str(self.msg+'五星级异常')
try:
    raise MyError('我其实没错')
except MyError as e:
    print(e)

例:

class Student():
    def __init__(self,name,sex,age,score):
        self.name=name
        self.sex=sex
        self.age=age
        self.score=score
zs=Student('张三','男',19,89)
print(dir(zs))
print(zs.name,zs.age,zs.sex,zs.score)

例:

class People():
    address='朝阳区'
    def __init__(self,name):
        self.name=name
zs=People('小红')
print(zs.name,zs.address)

xm=People('小明')
print(xm.name,xm.address)

zs.address='海淀'
print(zs.name,zs.address)
print(xm.name,xm.address)

print(zs.name,zs.address)
delattr(zs,'address')
print(zs.name,zs.address)

例:

class A():
    def hehe(self):
        print(self.name)

class B(A):
    def __init__(self,name):
        self.name=name

b=B('zhangsan')
b.hehe()

例:

class A():
    def __init__(self,name):
        self.name=name
def haha():
    print('hello')
a=A('jerry')
a.haha1=haha
a.haha1()

例:

class A():
    money=100000
    def __init__(self,name):
        self.name=name
    @classmethod
    def clsM(cls):
        print(cls.money)
        print(A.money)
A.clsM()
b=A('xiaohong')
b.clsM()
print(isinstance(b,A))

单例模式

class A():
    def __new__(cls, *args, **kwargs):
        if not hasattr(A,'dog'):
            cls.dog=object.__new__(cls)
        return cls.dog
    def __init__(self,name):
        self.name=name

a=A('haha')
b=A('heihei')
c=A('xixi')
print(a.name)
print(a==b)
print(a is b)


import os
os.mkdir('a',0x777)
cwd=os.getcwd()
os.listdir()
file_list=os.listdir()
bool=os.path.exists('.txt')

import os
a=input('输入一段字符:')
p=os.getcwd()
for i in a:
    p=p+'\\'+i #p=os.path.join(p,i)
    if not os.path.exists(p):
        os.mkdir(p,0x777)

猜你喜欢

转载自blog.csdn.net/Liang_Ren_/article/details/88541229