脚本专栏 发布日期:2025/2/22 浏览次数:1
本文实例讲述了Python嵌套函数,作用域与偏函数用法。分享给大家供大家参考,具体如下:
def my_pr1(): print("第一层打印") def my_pr2(): print("第二层打印") my_pr2()#如果在函数体内不调用内嵌的函数,那么无法在外部调用 my_pr1()
local(局部作用域) -->enclosing(函数范围作用域)-->global(全局作用域)--->build-in(内建对象作用域)
代码块级别的作用域: Python没有划分代码块作用域
if 1>0: name="automan" print(name)
上述代码运行结果:
automan
函数基本的作用域:
a=50 def change(x): x=6 change(a) print("after change:",a)
上述代码运行结果:
after change: 50
def func3(): superman="automan" print(superman)
运行结果:
NameError: name 'superman' is not defined
def func4(): superman="automan" def haha(): print(superman) haha() func4()
运行结果:
automan
aotuman='金甲战士' def f5(): print(aotuman) def f4(): aotuman='max' f5() f4()
运行结果:
金甲战士
import functools print_t=functools.partial(print,end='\t') print_t(1) print_t(1) print_t(1)
上述代码结果:
1 1 1
关于Python相关内容感兴趣的读者可查看本站专题:《Python函数使用技巧总结》、《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。