# -*- coding: utf-8 -*-
"""
Created on Fri Sep 7 09:13:50 2018 @author: zhen
"""
# 默认参数,必须在参数定义末尾
def mrcs(name, sex, age=18):
print(name, sex, age) print('*'*15+'默认参数'+'*'*15)
mrcs('liming','man')
mrcs('wangli', 'woman', 23)
print('='*15+'命名参数'+'='*15)
# 命名参数
def mmcs(name, sex, age):
print(name, sex, age) # 传统参数
mmcs('zhangsan', 'man', 34)
# 命名参数
mmcs(name='lisi', sex='man', age=12)
# 组合使用时传统参数必须按照参数定义的顺序
mmcs('bibi', age=23, sex='woman')
# mmcs(age=23, 'bibi', sex='woman') : SyntaxError: positional argument follows keyword argument print('-'*15+'可变参数'+'-'*15)
# 可变参数,元组
def kbcs(name, sex, age, *args):
print(name,sex,age)
print(args) # 不向args传参
kbcs('lili', 'woman', 18)
# 传入单个参数
kbcs('lili', 'woman', 18, 'beautiful')
# 传入多个参数
kbcs('lili', 'woman', 18, 'beautiful', 'fradom', 2018) print('+'*10+'键值对类型的可变参数'+'+'*10)
# 键值对类型的可变参数,字典
def kb2cs(name, sex, age, **kwargs):
print(name,sex,age)
print(kwargs) # 不向args传参
kb2cs('kk', 'man', 21)
# 传入单个参数
kb2cs('kk', 'man', 21, character='childhood')
# 传入多个参数
kb2cs('kk', 'man', 21, character='childhood', hobby='free', birthyear=2013) print('^'*15+'综合使用'+'^'*15)
# 综合使用,默认参数,命名参数,可变参数和键值对类型的可变参数
def comprehensive(name, sex='nan', *args, **kwargs):
print(name, sex, args, kwargs) # SyntaxError: positional argument follows keyword argument :命名参数和键值对类型的可变参数不能同时使用,会产生歧义
# comprehensive('liming', sex='man', 'student', 23, hobby='flying', idle=4)
comprehensive('liming', 'man', 'student', 23, hobby='flying', idle=4)
print('*'*37)
结果: