zoukankan      html  css  js  c++  java
  • Python之路【第三篇】:Python基础(17)——函数动态参数

    #动态参数:** 2星默认将传入的参数,全部放置在字典中f1(**{"kl":"v1", "k2":"v2"})
    #带2星的用来处理那些带有键值对的值,即一个key一个value的值

    # 示例
    # def func(**kwargs):
    # print(kwargs,type(kwargs))
    # # 执行方式一
    # func(name='wupeiqi',age=18)
    # # 执行方式二
    # li = {'name':'wupeiqi', 'age':18, 'gender':'male'}
    # func(**li)
    # # 输出
    # # {'age': 18, 'name': 'wupeiqi'} <class 'dict'>
    # # {'age': 18, 'gender': 'male', 'name': 'wupeiqi'} <class 'dict'>

    # # 练习1
    # def f1(**args): #两星 的类型是一个字典,字典的一个元素是键值对key-value,必须用指定参数来传值。
    # print(args,type(args))

    # f1("hello") #两星的情况下,传递一个参数会报错。
    # 输出报错:
    '''
    Traceback (most recent call last):
    File "C:/Users/Jam/PycharmProjects/s13/d3v1/12_函数8_动态参数_2星是字典.py", line 11, in <module>
    f1("hello") #
    TypeError: f1() takes 0 positional arguments but 1 was given
    '''

    # # 练习2
    # def f1(**args): #
    # print(args,type(args))
    # f1(n1="hello") #这相当于一个指定参数,作为字典的一个键值对,把n1当做key,把"hello"作为value
    # 输出
    # {'n1': 'hello'} <class 'dict'>

    # # 练习3
    # def f1(**args): #
    # print(args,type(args))
    # f1(n1="hello",n2=18) #作为字典的两个键值对,n1是key,n2是key
    # 输出
    # {'n2': 18, 'n1': 'hello'} <class 'dict'>

    # # 练习3
    # def f1(**args): #
    # print(args,type(args))
    # dic = {'k1':'v1','k2':'v2'}
    # f1(kk=dic) #kk当做一个key,字典dic当做一个value
    # 输出
    # {'kk': {'k2': 'v2', 'k1': 'v1'}} <class 'dict'>

    # # 练习4 注:如果形式参数有2星,实际参数也有2星的情况,相当于是直接赋值,dic是什么值args就是什么值。
    # def f1(**args): #
    # print(args,type(args))
    # dic = {'k1':'v1','k2':'v2'}
    # f1(**dic) #实际参数有2星
    #
    # # # 输出
    # # # {'k1': 'v1', 'k2': 'v2'} <class 'dict'>
  • 相关阅读:
    体验最火的敏捷——SCRUM(厦门,2014.1.4)
    再谈僵尸大会(每日会议)
    神马是敏捷?(4)——敏捷不能当饭吃
    XMLHttpRequest
    xml基础
    element 更换主题
    css3中新增的背景属性和文本效果
    transform rotate旋转 锯齿的解决办法
    优化实现Mobile/Bumped Diffuse
    优化实现Mobile Diffuse动态直接光照shader
  • 原文地址:https://www.cnblogs.com/jiangnanmu/p/5536698.html
Copyright © 2011-2022 走看看