python day17

Posted zm419914

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python day17相关的知识,希望对你有一定的参考价值。

一.函数整体部分回顾总结。

1.函数的传参

# def func(x,y):
# x += 2
# return x + y
# print(func(4,3))
# func(y=4,x=3,)

# x = 2
# y = 3
# def func1():
# global x
# x += 2
# return x + y
# func1()
# ,上面的例子是传参可以在函数内改变参数的值,但是像第一种,只能引用.

# def func1(x,y):
# x = 3
# y = 2
# return x + y
# x = 2
# y = 3
# func1(y,x)

# def func1(x,y,z,w):
# return x + y
#
# func1(2,3,4,w=6)
# 形参角度:
# 默认参数
# def func(x,y,sex=‘男‘):
# x += 2
# print(sex)
# return x + y
# print(func(4,3,sex=‘laddyboy‘))

# def func2(*args,**kwargs):
# print(args)
# print(kwargs)
# func2(1,2,3,a=2,c=6,d=7)

# 形参角度的所有参数的顺序:
# 位置参数,*args,默认参数,**kwargs
# def func(x,y,*args,sex=‘男‘):
# print(x)
# print(y)
# print(sex)
# print(args)
# func(1,2,55,66,‘alex‘)
# def func(x,y,*args,sex=‘男‘,**kwargs):
# print(x)
# print(y)
# print(sex)
# print(args)
# print(kwargs)
# func(1,2,55,66,‘alex‘,sex=‘nv‘,name=‘alex‘)

# * ** 魔法符号
# 1,用法
# 在函数的执行(调用)时,* ** 打散.
# 在函数的定义时,* ** 聚合.

# def func3(*args, **kwargs):
# args = (1, 2, 3)
# print(*args) # print(1,2,3)
# print(**kwargs)#
# l1 = [1, 2, 3]
# l2 = [4, 5, 6]
# l3 = (4, 5, 6)
# func3(*l1, *l2, *l3) # func3(1,2,3,4,5,6,4,5,6)

# print(1,dic = {‘1‘: ‘oldboy‘, ‘age‘: 1000}
# func3(**dic) <==> func3(‘1‘=‘oldboy‘,age=1000)
# func3(*dic)
# func3(name =‘oldboy‘,age=1000)
# func3(**dic)
# print({‘name‘: ‘oldboy‘,‘name‘:‘alex‘,‘name‘:"太白"})
# func3(1,2,3,name=‘alex‘)2,3)


3.空间
# b = 5
# def func():
# def inner():
# print(input)
# inner()
# func()

# a = 3
# b = 5
# print(globals())
# def func():
# a = 666
# b = 777
# # print(globals())
# # print(locals())
# def inner():
# pass
# inner()
#
# func()

# print(globals()) # 全局作用域的所有内容.
# print(locals())

# 相关面试题
# a = 1
# def func():
# # 局部定义的a
# a = a + 1 # 局部作用域不能修改全局作用域的变量,只能引用
# print(a)
# func()
# 局部作用域对全局作用域的变量可以进行引用,但是不能修改,
#局部作用域对全局作用域的变量进行引用并修改,赋值给了一个同名的变量,
#那么python就会认为你这个引用的变量是局部作用域定义的,但是局部没有,所以他会报错

# def func():
# # 局部定义的a
# a = 3
# def inner():
# a = a + 1 # 局部作用域不能修改全局作用域的变量,只能引用
# print(a)
# inner()
# func()
# global
# nonlocal
# name = ‘oldboy‘
# def func1():
# global name
# name = ‘alex‘
# print(name)

# def func1():
# global a
# a = 1
# def inner():
# nonlocal a
# a += 1
# print(a)
# # def inner1():
# # a += 1
# print(a)
# inner()
# print(a)
# func1()
# 1,nonlocal 对上级变量进行修改.
# nonlocal 不能操作全局变量
# a = 10
# def func1():
# global a
# a = 1
# def inner():
# a += 1
# print(a)
# def inner1():
# a += 1
# inner1()
# print(a)
# inner()
# print(a)
# func1()

# flag = True
# while flag:
# print(111)
# flag = False
# golbal nonlocal 仅限于不可变的数据类型

# l1 = [1,2,3]
# def func3():
# l1.append(666)
# print(l1)
# func3()
# print(l1)

#函数名:
‘‘‘
1,函数名可以赋值.
2,函数名可以当做容器类的元素.
‘‘‘
# def func1():
# print(666)
# f1 = func1
# f1()
# def func1():
# print(555)
# def func2():
# print(666)
# l1 = [func1,func2]
# for i in l1:
# i()

#闭包:内存函数对外层函数变量(非全局变量的引用)

# 装饰器.
#
# def wrapper(f): # f = func1(func1函数的内存地址)
# def inner(*args,**kwargs):
# #args = (1,2,)
# print(‘666‘)
# ret = f(*args,**kwargs) # f(*(1,2,)) f(1,2,) 3
# print(‘777‘)
# return ret # 3
# return inner
#
# @wrapper # func1 = wrapper(func1) # inner
# def func1(x,y):
# return x + y
# func1 = wrapper(func1)
# print(func1(1,2)) # inner(1,2,)

# python2x python3x 功能的区别



#带参数的装饰器

# def out_wrapper(*args,**kwargs):
# def wrapper(f):
# def inner(*args,**kwargs):
# print(‘666‘)
# ret = f(*args,**kwargs)
# print(‘777‘)
# return ret
# return inner
# return wrapper
#
#
# @out_wrapper(1,2,3) # 1 执行out_wrapper(1,2,3) 返回一个wrapper 2, @wrapper func1 = wrapper(func1)
# def func1(x,y):
# return x + y
#
# func1()

# flag = True
# flag = True
# flag = True
# flag = True
# def wrapper(f): # f = func1(func1函数的内存地址)
#
# global flag
# if flag:
# def inner(*args,**kwargs):
# #args = (1,2,)
# print(‘666‘)
# ret = f(*args,**kwargs) # f(*(1,2,)) f(1,2,) 3
# print(‘777‘)
# return ret # 3
# else:
# ret = f(*args, **kwargs) # f(*(1,2,)) f(1,2,) 3
# return ret # 3
# return inner

























































































































































































































































以上是关于python day17的主要内容,如果未能解决你的问题,请参考以下文章

人生苦短,我用python-- Day17 jQuery讲解

python 片段17

python学习点滴记录-Day17-django-part2

day17 内置函数

python15-day17

Python学习day17 任务发布