zoukankan      html  css  js  c++  java
  • Python:函数参数*args 和**kwargs,pdb调试模块介绍

    # 参数*args 和**kwargs不是必须这样写,只有变量前⾯的 *(星号)才是必须的.
    # *args 是⽤来发送⼀个⾮键值对的可变数量的参数列表给⼀个函数
    # 下面的例子理解这个意思
    def test_var_args(f_arg, *args):
    print("first normal arg:", f_arg)
    for arg in args:
    print("another arg through *argv:", arg)

    #test_var_args('Michael', 'python','eggs','test')

    # **kwargs 允许将不定长度的键值对, 作为参数传递给⼀个函数。 可在⼀个函数⾥处理带名字的参数,
    def greet_me(**kwargs):
    for key,value in kwargs.items():
    print("{0} == {1}".format(key, value))

    #greet_me(name='Michael', age=23, sex='man')

    # 使⽤*args和**kwargs 来调⽤⼀个函数,先假设有下面这个函数
    def test_args_kwargs(arg1, arg2, arg3):
    print("arg1:", arg1)
    print("arg2:", arg2)
    print("arg3:", arg3)

    # args = ("two", 3, 5)
    # test_args_kwargs(*args) # 使用 *args给函数传递参数
    # kwargs = {"arg3": 3, "arg2": "two", "arg1": 5}
    # test_args_kwargs(**kwargs) # 使用 **kwagrs给函数传递参数,字典的键名必须与定义函数时的形参名一致

    # 调试(Debugging),Python debugger(pdb)能捕捉代码的bug
    # 命令行运行程序可使用如下方法,进入调式模式可用:n,q 按键
    # python -m pdb my_script.py
    import pdb

    def make_bread():
    pdb.set_trace()
    return "I don't have time"

    print(make_bread())
    # c: 继续执⾏
    # w: 显⽰当前正在执⾏的代码⾏的上下⽂信息
    # a: 打印当前函数的参数列表
    # s: 执⾏当前代码⾏,并停在第⼀个能停的地⽅(相当于单步进⼊)
    # n: 继续执⾏到当前函数的下⼀⾏,或者当前⾏直接返回(单步跳过)
    # 单步跳过(next)和单步进⼊(step)的区别在于, 单步进⼊会进⼊当前⾏调⽤的函数内
    # 部并停在⾥⾯, ⽽单步跳过会(⼏乎)全速执⾏完当前⾏调⽤的函数,并停在当前函数的
    # 下⼀⾏。
  • 相关阅读:
    使用Dictionary键值对判断字符串中字符出现次数
    Linq实现字符串拼接多条件查询
    js数据类型转换
    js前端数据类型检测typeof,instanceof,Object.prototype.toString.call
    moment.js格式化日期,获取前一个月的时间
    css 样式中height100%失效问题
    记一次react项目运行崩溃
    null和undefined区别
    windows腾讯云/阿里云服务器更换操作系统为linux
    csrf攻击原理和防御-生成token防御代码
  • 原文地址:https://www.cnblogs.com/Micro0623/p/9950491.html
Copyright © 2011-2022 走看看