python可变参数 *args 与**kwagrs

*args是可变的positional arguments列表,

**kwargs是可变的keyword arguments列表。

所谓positional argument位置参数,是指用相对位置指代参数。关键字参数(keyword argument),

见名知意使用关键字指代参数。位置参数或者按顺序传递参数,或者使用名字,自然使用名字时,对顺序没有要求。

 

def foo(x, y):
    return x*(x+y)
print(foo(1, 2))            # 3, 使用positional argument
print(foo(y=2, x=1))        # 3,named argument

  

 

在python中,当*和**符号出现在函数定义的参数中时,表示任意数目参数。

*args表示任意多个无名参数,类型为tuple;**kwargs表示关键字参数,为dict。

使用时需将*arg放在**kwargs之前,否则会有“SyntaxError: non-keyword arg after keyword arg”的语法错误。

元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示

tuple所谓的“不变”是说,tuple的每个元素,指向永远不变。

即指向'a',就不能改成指向'b',指向一个list,就不能改成指向其他对象,但指向的这个list本身是可变的!

 

def calc(*numbers):
    sum = 0
    for num in numbers:
        sum = sum + num
    print(sum)
calc(1,2,3,4)
输出:10

  

def d(**kwargs):
    print(kargs)
d(a=1,b=2)
输出:{'a': 1, 'b': 2}

  

 

上面是在函数定义的时候写的*和**形式,那反过来,如果*和**语法出现在函数调用中呢

 

#通过一个元组给一个函数传递四个参数,并且让python将它们解包成不同的参数。
def func(a,b,c,d):
    print(a,b,c,d)

a = (1,2,3,4)
func(*a)

输出:1 2 3 4
# 如果已经有一个元祖,在参数前加*,函数会把元祖中的元素一个一个传到函数里面 
def calc(*numbers):
  sum = 0
  for n in numbers:
    sum = sum + n * n
  print(sum)

num = (1,2,3,4)
calc(*num)

输出:10

#如果已经有一个dict,在参数前面加**,函数会把dict中所有键值对转换为关键字参数传进去
def person(name,age,**kw):
  print('name:',name,'age:',age,'other:',kw)

extra = {'city': 'Beijing', 'job': 'Engineer'}
person('Jack', 24, **extra)

输出:name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}

  

 

python 彩蛋

>>>import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

------------------------------------------------------------------

Python之禅 by Tim Peters
 
优美胜于丑陋(Python 以编写优美的代码为目标)
明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似)
简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)
复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)
扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)
间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题)
可读性很重要(优美的代码是可读的)
即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上)
 
不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写 except:pass 风格的代码)
 
当存在多种可能,不要尝试去猜测
而是尽量找一种,最好是唯一一种明显的解决方案(如果不确定,就用穷举法)
虽然这并不容易,因为你不是 Python 之父(这里的 Dutch 是指 Guido )
 
做也许好过不做,但不假思索就动手还不如不做(动手之前要细思量)
 
如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然(方案测评标准)
 
命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)

  

 

posted on 2018-05-27 16:59  Alex-zs  阅读(445)  评论(0编辑  收藏  举报

导航