* 和**主要用在函数的参数中,
# coding=utf8 """
三种编码:
1 python程序代码的编码
# coding=utf8
2 设置/查看python程序运行环境的编码
import sys
查看
sys.getdefaultencoding()
设置
reload(sys)
sys.setdefaultencoding('utf-8')
3 python程序读取外部文件,网页内容的编码
encode,decode
""" def function(*args, **kwargs):
"""
:param args: 表示任何多个无名参数,这些参数会以一个tupe的形式访问
:param kwargs: 用来处理任意个有名字的参数,这些参数用dict来访问
:return:
"""
print "args type = %s" % type(args)
print "args = %s" % str(args)
for i, item in enumerate(args):
print "args[%d] = %s" % (i, item) print "------------"
print "kwargs type = %s" % type(kwargs)
print "kwargs = %s" % kwargs
for k, v in kwargs.items():
print "%s:%s" % (k, v) function(1, 2, 3, name="liuzhipeng", sex="male")
结果为
C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/oldboy/lesson_3/44.py
args type = <type 'tuple'>
args = (1, 2, 3)
args[0] = 1
args[1] = 2
args[2] = 3
------------
kwargs type = <type 'dict'>
kwargs = {'name': 'liuzhipeng', 'sex': 'male'}
name:liuzhipeng
sex:male Process finished with exit code 0
* 和** 在打印元组(列表)和字典的时候很有用
print "i am {name}, and age is {age}".format(**{"name": "lzp", "age": 14})
print "i am {}, and age is {}".format(*("lzp",14))
i am lzp, and age is 14
i am lzp, and age is 14