python 基础语法 异常 模块 包

2024-01-09 06:53:36

异常捕获

try:
    f = open("./abc.txt","r",encoding="UTF-8")
except:
    print("==========")
    f = open("./abc.txt","w",encoding="UTF-8")

#指定异常
try:
    print(xxx_test)
except NameError as e:
     print(f"error={e}") 

#多个异常
try:
    #print(name)
    num = 10
    result = num/0
except (NameError,ZeroDivisionError) as e:
    print(f"tuple=error={type(e)}") 

#捕获所有异常
try:
    10/0
    print(xxxxxxxx)
except Exception as e:
    print(f"all=error={type(e)}") 

#无异常处理
try:
    print("hello world python")
except Exception as e:
    print(f"tuple=error={type(e)}") 

else:
    print("no exception")



#不管是否有异常都要执行的
try:
    print("hello world python")
except Exception as e:
    print(f"tuple=error={type(e)}") 

else:
    print("no exception")
finally:
    print("xxxxxxxxxxxxx")

异常传递性

模块

自定义模块

引入不同模块下相同的函数时候 后边引入的会把前边引入的覆盖掉

模块测试


def sum(a,b):
    return a + b

#__main__ 
if __name__ == '__main__':
   result = sum(23,45)
   print(result)

#from time import sleep
import time
import module01
import package01.moudle01 as mu
from package01 import moudle02
result = module01.sum(123,456)
print(result)
result = mu.mulity(22,33)
print(result)

moudle02.say_say("我说 你输出")
#time.sleep(10)

?

导入三方包?

1.在需要的文件中导入包名

1. 使用pip install (pip 包管理工具 类似 iOS 的pod)

文章来源:https://blog.csdn.net/weixin_38432324/article/details/135468924
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。