python 知识点记录

repr(): 将字典转化为字符串形式,相当于json.dumps()

函数传值,注意[传值]还是[传指针],列表、字典都是传指针

函数 - 默认参数

def func(a, b=3):
	print(a, b)

函数 - 不定长参数

def func(a, *b):
	for i in b:
		print(i)

func(1, 2, 3, 4)

函数 - 匿名函数

result = lambda a, b : a + b
print(result(1, 2))


global:声明全局变量;nonlocal:改变嵌套作用域

num = 1

def func():
	global num

	print(num)
	num = 123
	print(num)

func()
print(num)


def outer():
    num = 10

    def inner():
        nonlocal num   # nonlocal关键字声明
       	
        num = 100
        print(num)

    inner()
    print(num)

outer()


猜你喜欢

转载自blog.csdn.net/chenjineng/article/details/80734688