class Student():
@property
def name(self): #获取
print('我在这儿,哈哈')
return self._name+'678'
@name.setter
def name(self,x): #设置方法
if len(x)>5:
print('一切皆有可能')
self._name=x[:6]
else:
self._name=x
zs=Student()
zs.name='12345'
print(zs.name)
异常处理
a=[1,2]
try: #把容易出错的代码放在 try(尝试内)
print(a[2])
except IndexError as b: # 当有错误的时候捕捉到异常,进行处理
print(b,'错误')
print(a[0])
a=[1,2,3]
b=iter(a)
while True:
try:
print(next(b))
except StopIteration as e:
print(e)
break
a=[1,2,3]
try:
print(a[4])
print(1/0)
except IndexError as e:
print(1,'下标越界!')
except ZeroDivisionError as e:
print(2,'除零异常!')
print(3,'哈哈')
a=[1,2,3]
try:
print(a[1])
print(1/1)
except IndexError as e:
print(1,'下标越界!')
except ZeroDivisionError as e:
print(2,'除零异常!')
else:
print('没异常走else!')
print(3,'哈哈')
a=[1,2,3]
try:
print(a[4])
print(1/0)
except(IndexError,ZeroDivisionError) as e:
print(1,'任意一个有异常都报错!')
else:
print('哈哈哈')
a=[1,2,3]
try:
print(a[0])
except (IndexError,ZeroDivisionError) as e:
print(1)
else:
print(2,'要是没异常才执行!')
finally:
print(3,'无论对错,finally都执行!')
print(4)
a=[1,2,3]
try:
print(a[4])
print(1/0)
except: #任何异常都捕捉
print('万能异常处理')
a=[1,2,3]
try:
raise IndexError('主动抛出异常') #括号内为其内容
except IndexError as f:
print('异常!')
class Beauti(Exception):
def __init__(self,msg):
self.msg=msg
def __str__(self):
return '哈哈哈'+str(self.msg)
try:
raise Beauti('呵呵呵')
except Beauti as f:
print(str(f))
导入包
from ~~ import * 这是导出所有文件
__all__=() ~~from ~~import #这是选择性的导入
主函数
if __name__=='__main__':
print('我是主函数',__name__)
else:
print('我不是主函数',__name__)
09-09