zoukankan      html  css  js  c++  java
  • Python函数的动态参数

    def  show(arg):

       print(arg)

    n=[11,22,33]

    show(n)

    def show(*arg):

      print(arg,type(arg))

    show(1,11,22,33)-------------------(1,11,22,33) tuple

    def show(**arg):

      print(arg,type(**arg))

    show(n1=78,n2=23)-------------------{'n1':78,'n2':23}dict

    def show(*args,**kwargs):

      print(args,type(args))

      print(kwargs,type(kwargs))

    show(c,n1=88,zy='sb')---------(11,22,33) tuple{ 'n1':88,'zy':'sb' }dict

    l=[11,22,33,]

    d={'n1':88,'zy':'sb'}

    show(*l,**d)

    使用动态参数实现字符串格式化:

    s1="{0} is{1}"

    ret=s1.format('alex','2b')

    l=[('alex','sb']

    ret=s1.format(*l)

    print(ret)

    s1="{name} is{acter}"

    ret=s1.format(name='alex',acter='2b')

    d={'name':'alex','acter':'2b'}

    ret=s1.format(**d)

    print(ret)

  • 相关阅读:
    20151104内置对象
    20151102adonet2
    20151029adonet1
    20151028c#4
    20151027c#3
    20151026c#2
    20151021c#1
    20151020sql2
    20151019sql1
    Codeforces Round #261 (Div. 2) C. Pashmak and Buses(思维+构造)
  • 原文地址:https://www.cnblogs.com/my334420/p/6346964.html
Copyright © 2011-2022 走看看