Python数据分析script必备知识(五)
1.Python中传参的四种参数类型
"""
函数的参数
1.位置参数 def test1(x,y): x,y就是位置参数
2.默认参数 def test2(x,y=1) y就是默认参数
3.可变参数 def test3(*args) 函数内部参数可变,收到的是tuple
4.关键字参数 def test4(**Kwargs) 允许不定长的键值对,自动组装为一个dict
"""
# 可变参数
def func1(f,*args):
print(f,type(f))
print(args,type(args))
nums = [4,5,6]
func1(1,2,*nums)
li = '测试'
func1(2,*li)
# 4.关键字参数
def func2(name,age,**kwargs):
print('name:',name,'age:',age,'other:',kwargs,type(kwargs))
func2('jack',30,city='shanghai')
2.装饰器的实际使用场景
1.基本装饰器(装饰一个不带参数的函数)
"""
Python装饰器高级版————Python类内定义装饰器并传递self参数
本测试重点:解决类里面定义的装饰器,在同一个类里面使用的问题, 并实现了装饰器的类熟悉参数传递
"""
# 基本装饰器 (装饰不带参数的函数)
def clothes(func):
def wear():
print('Buy clothes!{}'.format(func.__name__))
return func()
return wear
@clothes
def body():
print('The body feels could!')
body()
# 备注:@ 是语法糖
# 不用语法糖的情况下,使用下面语句也能实现装饰作用:把dody再加工,再传给body
# 例如
# def clothes(func):
# def wear():
# print('Buy clothes!{}'.format(func.__name__))
# return func()
# return wear
# def body():
# print('The body feels could!')
# body = clothes(body)
# body()
2.装饰一个带参数的函数
"""
装饰一个带参数的函数
"""
def clothes(func):
def wear(anything): # 实际是定义一个anything参数,对应body函数参数
print('Buy clothes! {}'.format(func.__name__))
return func(anything) # 执行func函数,并传入调用传入的anything参数
# wear = func(anything) # 在这一步,实际上可以看出来,为啥wear必须带参数, 因为它就是func(anything)
return wear
# 所以clothes的结果是
# clothes = wear = func(anything)
# 不用语法糖的情况下就是
# body = clothes(body)('hands')
# 进一步证明:print(body.__name__) 显示的是wear函数
@clothes
def body(part):
print('The body feels could! {}'.format(part))
body('hands')
3.装饰不定长参数的函数
"""
装饰不定长参数的函数
通常装饰器不只装饰一个函数,每个函数参数的个数也不相同
这个时候使用不定长参数 *args,**kwargs
"""
def clothes(func):
def wear(*args,**kwargs):
print('Buy clothes ! {}'.format(func.__name__))
return func(*args,**kwargs)
return wear
@clothes
def body(part):
print('The body feels could! {}'.format(part))
@clothes
def head(head_wear,num=2):
print('The head need buy {} {}!'.format(num,head_wear))
body('heads')
head('headdress')
4.装饰器带参数
"""
装饰器带参数
把装饰器再包装,实现了seasons传递装饰器参数————————三层
"""
def seasons(season_type):
def clothes(func):
def wear(*args,**kwargs):
if season_type == 1:
s = 'spring'
elif season_type == 2:
s = 'summer'
elif season_type == 3:
s = 'autumn'
elif season_type == '4':
s = 'winter'
else:
print('The args is error!')
return func(*args,**kwargs)
print('The season is {}!{}'.format(s,func.__name__))
return func(*args,**kwargs)
return wear
return clothes
@seasons(2)
def children():
print('i am children')
children()
5.类里定义装饰器 ,把装饰器写在类里
"""
二、在类里定义装饰器,装饰本类内函数
类装饰器,装饰函数和类函数调用不同的类函数
(1)把装饰器写在类里
在类里面定义个函数,用来装饰其他函数,严格意义上说不属于类装饰器
"""
class Buy(object):
def __init__(self, func):
self.func = func
# 在类里定义一个函数
def clothes(func): # 这里不能用seLf,因为接收的是body函数
# 其他都和普通的函数装饰器相同
def ware(*args, **kwargs):
print('This is a decrator!')
return func(*args, **kwargs)
return ware
@Buy.clothes
def body(hh):
print('This body feels could!{}'.format(hh))
body('hh')
6.类里面定义装饰器 ,装饰器装饰同一个类的函数
"""
二、在类里面定义装饰器,装饰本类内函数
(2) 装饰器装饰同一个类的函数
背景:想要通过装饰器修改类里的self属性值
"""
class Buy(object):
def __init__(self):
self.reset = True # 定义一个类属性,稍后在装饰器里更改
self.func = True
# 在类里面定义一个装饰器
def clothes(func): # func 接收body
def ware(self,*args,**kwargs): # self,接收body里的self,也就是类实例
print('This is a decrator!')
if self.reset == True: # 判断类属性
print('Reset is Ture,change Func..')
self.func = False # 修改类属性
else:
print('reset is False.')
return func(self,*args,**kwargs)
return ware
@clothes
def body(self):
print(