39 py function scope recursive function local variable scoping function uses lambda

第十课:函数作用域
// python 中的嵌套函数  在一个函数中再定义一个函数
# 小结 :
# 函数作用域:因为:python是动态语言,定义变量的时候是不需要指定变量类型的,这样的话,我们在使用或者定义变量的时候作用域会分不清
# 如果在函数中定义一个变量,而且变量名和该函数上一级的作用域中的变量名相同
# 那么在该函数使用该变量时,就会使用局部变量
# 如果在函数中使用一个变量,但该变量在函数中并没有定义,那么会到该函数上一层的作用域去寻找该变量,如果还没有找到,会继续到上一层作用域去寻找,如果没找到会抛出变量未定义异常
x = 10          # 定义了一个变量 并赋值 
def fun1():
    x = 100
fun1()
print(x)         # 10

y = 123
def fun2():
    print(y)
fun2()            # 123 在函数中,如果在函数体中没有定义变量的话,首先会在函数体中去找 变量的值,如果没有,就找全局的作用域去找  

n = 332
def fun3():
    n = 4
    print(n)
fun3()            # 4   这个和第一个例子有什么区别呢? 多了一个 print(n) 其实这个就是局部作用域,在调用函数的时候就已经算出值了。

def fun4():
    print(n)
    n = 100
# fun4()  抛出异常

# 定义一个嵌套函数 
m = 10
def fun5():
    # m = 100
    def fun6():
        print(m)
        print('fun6')
    return fun6           # 反映 函数的引用 
fun5()()           # 100 fun6   调用函数fun6的引用      比如在fun6这个函数中没有定义m 那么就会在上一层 m = 100 找 找到了100 那么就输出100 接下来 如果注释掉m = 100 那么就需要去上一层再找 找了 m = 10 那么就输出10    如果再注释了,那么就找不到了 就会报错

----------------------------------------------------------
第11课:函数的递归
# 函数递归:在一个函数中调用函数本身    自己调用自己 

# 阶乘
# n! = 1 * 2 * 3 * ... *n
# n! = (n - 1)! * n  n == 0 or n == 1
def jc(n):
    # 终止条件
    if n == 0 or n == 1:
        return 1           # 返回结果为1 
    else:
        return jc(n - 1) * n
print(jc(10))   # 3628800

# 斐波那契数列   : 当前的数列值,表示前2项数列之和
# 0 1 1 2 3 5 8 13 21 
# f(n) = f(n - 1) + f(n - 2)   
# n == 0  return 0    n == 1 return 1
def fibnonacci(n):
    # 终止条件
    if n == 0:
        return 0      # 直接返回0
    elif n == 1:
        return 1      # 直接返回1 
    else:
        return fibnonacci(n - 1) + fibnonacci(n - 2)

print(fibnonacci(8))    # 21 

--------------------------------------------------------------------------
第十四课: python变量作用域
局部变量: 比如在函数体内 有用 
全局变量: 在整个范围内都有用
在py中有3个函数 可以获取局部变量和全局变量
globals: 获取全局范围内所有的变量
locals: 获取当前作用域内的所有变量
vars(object): 获取指定对象范围内所有的变量,如果不指定object ,vars和locals的作用是完全一样的

def hanshu():
    name = 'majihui'
    age = 30
    print(name,age)    # majihui 30
    print(locals())    # {'name': 'majihui', 'age': 30} 转化为了字典
    #print(globals())  #{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7fb62815d7b8>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/majihui/pycharm_work/test07.py', '__cached__': None, 'hanshu': <function hanshu at 0x7fb6280dc268>}
    # 全局变量输出的什么鬼,太多了
    print(vars())      # {'name': 'majihui', 'age': 30}
    #print(vars(object)) # {'__repr__': <slot wrapper '__repr__' of 'object' objects>, '__hash__': <slot wrapper '__hash__' of 'object' objects>, '__str__': <slot wrapper '__str__' of 'object' objects>, '__getattribute__': <slot wrapper '__getattribute__' of 'object' objects>, '__setattr__': <slot wrapper '__setattr__' of 'object' objects>, '__delattr__': <slot wrapper '__delattr__' of 'object' objects>, '__lt__': <slot wrapper '__lt__' of 'object' objects>, '__le__': <slot wrapper '__le__' of 'object' objects>, '__eq__': <slot wrapper '__eq__' of 'object' objects>, '__ne__': <slot wrapper '__ne__' of 'object' objects>, '__gt__': <slot wrapper '__gt__' of 'object' objects>, '__ge__': <slot wrapper '__ge__' of 'object' objects>, '__init__': <slot wrapper '__init__' of 'object' objects>, '__new__': <built-in method __new__ of type object at 0x1034b15e8>, '__reduce_ex__': <method '__reduce_ex__' of 'object' objects>, '__reduce__': <method '__reduce__' of 'object' objects>, '__subclasshook__': <method '__subclasshook__' of 'object' objects>, '__init_subclass__': <method '__init_subclass__' of 'object' objects>, '__format__': <method '__format__' of 'object' objects>, '__sizeof__': <method '__sizeof__' of 'object' objects>, '__dir__': <method '__dir__' of 'object' objects>, '__class__': <attribute '__class__' of 'object' objects>, '__doc__': 'The most base type'}
    # 输出的什么鬼
    print(locals()['age'])  # 30
    locals()['age'] = 50    # 尝试去修改,结果为 不会去修改参数值 并不是变量本身
    print(age)              # 30

hanshu()

x = 20
y = 40

print(globals()['x'])    # 20

#我们接下来,可以定义一个对象
class myclass():
    def __init__(self):
        self.name = 'majihui'
print(vars(myclass()))        # {'name': 'majihui'}

#如何在一个函数中使用全局变量
value = 100
def a():
    value = 200     # 定义了一个新的局部变量
    print(value)
a()                 # 200

-------------------------------------------------------------------------
第十五课:局部函数
局部变量只在函数的内部起作用,
局部函数和局部的变量是一样的,只有在函数的内部才能被定义,才能被调用

def process(type,n):
    def pinfang(n):
        return n * n
    def lifang(n):
        return n * n * n
    def add(n):
        return n + n

    if type == 'pinfan':
        return pinfang(n)
    elif type == 'lifang':
        return  lifang(n)
    else:
        return add(n)

print(process('pinfang',10))     
print(process('lifang',10))
print(process('add',10))
结果为:
20
1000
20

def xyz():
    name = 'majihui'
    def x():
        #print(name)
        name = 'mjh'
        print(name)
    x()
xyz()    # mjh

def xyz():
    name = 'majihui'
    def x():
        nonlocal name
        print(name)
        name = 'mjh'
        #print(name)
    x()
xyz()    # majihui 

----------------------------------------------------------------------
第十六课 使用函数变量
在python语言中,可以将函数当成一个变量使用,可以将一个函数傅给变量

# 这一步将一个函数付给另外一个变量的方式
def pow(base,exponent):
    result = 1
    for i in range(1,exponent +1):
        result *= base
    return result

print(pow(2,10))   # 1024 

f = pow

print(f(3,10))   # 59049 

# 将函数本身最为一个参数
def area(width,height):
    return width * height
f = area

print(f(3,4))  # 12

# 这个area函数也可以作为一个函数的参数

def process(fun,a1,a2):
    return fun(a1,a2)

print(process(pow,4,5))    # 1024
print(process(area,10,20))  # 200 

# 将函数本身作为一个返回值
print("-----------")
def process1(type):
    def square(n):
        return n * n
    def add(n,m):
        return n + m
    if type == 'square':
        return square
    else:
        return add

print(process1('square')(12))     # 144
print(process1('add')(12,43))      # 55

---------------------------------------------------------------------
第十七课 使用lambda表达式代替局部函数
# 使用lambda表达式代替局部函数
# lambda表达式本身就是一个表达式,这个表达式他可传入一个参数,可以有1行的执行代码
# 他实际上就是一个简化的函数

def get_math_func(type):
    if type == 'sequare':
        return lambda n:n * n
    elif type == 'cube':
        return lambda n:n * n * n
    else:
        return lambda n:n + n

math_func01 = get_math_func('sequare')
print(math_func01(10))                              # 100
math_func02 = get_math_func('cube')
print(math_func02(10))                              # 1000
math_func03 = get_math_func('add')
print(math_func03(10))                              # 20 
# 调用的方式和其他的都是一样的 

Guess you like

Origin blog.51cto.com/12445535/2465199