上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 15 下一页
摘要: def func(num, div): assert (div != 0), "div不能为0" return num / divprint(func(10, 0))div不等于0 才能运行成功 阅读全文
posted @ 2020-02-04 21:10 i勤能补拙 阅读(189) 评论(0) 推荐(0) 编辑
摘要: '''try……except……finally格式:try: 语句texcept 错误码 as e: 语句1except 错误码 as e: 语句2……except 错误码 as e: 语句nfinally: 语句f作用:语句t无论是否有错误都讲执行最后的语句f'''try: print(1/1)e 阅读全文
posted @ 2020-02-04 20:53 i勤能补拙 阅读(204) 评论(0) 推荐(0) 编辑
摘要: num = 10print(id(num))def func(): #声明num为全局变量,方便在函数中修改 global num # 修改num num = 20 print(id(num)) #可以使用,但是无法直接修改 #num = 20#相当于在函数内部定义了一个num #print(id( 阅读全文
posted @ 2020-02-04 20:48 i勤能补拙 阅读(1714) 评论(0) 推荐(0) 编辑
摘要: 第一,两者的功能不同。global关键字修饰变量后标识该变量是全局变量,对该变量进行修改就是修改全局变量,而nonlocal关键字修饰变量后标识该变量是上一级函数中的局部变量,如果上一级函数中不存在该局部变量,nonlocal位置会发生错误(最上层的函数使用nonlocal修饰变量必定会报错)。 第 阅读全文
posted @ 2020-02-04 20:45 i勤能补拙 阅读(507) 评论(0) 推荐(0) 编辑
摘要: '''def outer(): num = 10 def inner(): #修改num nonlocal num num = 20 print("在inner里打印num =", num) inner() print("在outer里打印num =", num)outer()'''def oute 阅读全文
posted @ 2020-02-04 20:09 i勤能补拙 阅读(355) 评论(0) 推荐(0) 编辑
摘要: 局部-》函数-》全局-》内建python中只有模块、函数、类才会引入新的的作用域去,其他的(if/if-elif-else/while/for)等不会引入新的作用域的'''if 1: a = 10print(a)#print(num)#体现作用域def func(): b = 20 print("b 阅读全文
posted @ 2020-02-04 11:04 i勤能补拙 阅读(317) 评论(0) 推荐(0) 编辑
摘要: import functools#print(int("1010", base = 2))#偏函数def int2(str, base = 2): return int(str, base)print(int2("1011"))#把一个参数固定住,形成一个新的函数int3 = functools.p 阅读全文
posted @ 2020-02-04 11:02 i勤能补拙 阅读(152) 评论(0) 推荐(0) 编辑
摘要: def outer(func): def inner(*args, **kwargs): #添加修改的功能 print("&&&&&&&&&&&&&") return func(*args, **kwargs) return inner@outer #say = outer(say)def say( 阅读全文
posted @ 2020-02-03 15:41 i勤能补拙 阅读(446) 评论(0) 推荐(0) 编辑
摘要: def outer(func): def inner(age): if age < 0: age = 0 func(age) return inner#使用@符号将装饰器应用到函数#@python2.4支持使用@符号@outer #相当于say = outer(say)def say(age): p 阅读全文
posted @ 2020-02-02 21:36 i勤能补拙 阅读(177) 评论(0) 推荐(0) 编辑
摘要: '''概念:是一个闭包,把一个函数当做参数,返回一个替代版的函数,本质上就是一个返回函数的函数'''#简单的装饰器def func1(): print("sunck is a good man")def outer(func): def inner(): print("*************** 阅读全文
posted @ 2020-02-02 18:40 i勤能补拙 阅读(162) 评论(0) 推荐(0) 编辑
上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 15 下一页