Python数据分析script必备知识(五)

本文详细介绍了Python数据分析脚本中装饰器的使用,包括装饰无参数函数、带参数函数、不定长参数函数以及装饰器的参数化。同时,讲解了类装饰器的概念、应用场景和相关知识点。此外,还涉及了Python的不定长参数传参、高阶函数用法、DataFrame操作、Outlook邮件发送、字符串比较以及selenium的版本差异、驱动安装和自定义事件触发等实用技巧。
摘要由CSDN通过智能技术生成

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(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今晚务必早点睡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值