python 关键字参数

2018-02-06  本文已影响0人  橙姜

def person(name,age,**kw):
print('name:',name,'age:',age,'other:',kw)
person('Frank','37')
person('Frank','37',city='Shanghai')
person('Frank','37',gender='M',job='Engineer')


('name:', 'Frank', 'age:', '37', 'other:', {})
('name:', 'Frank', 'age:', '37', 'other:', {'city': 'Shanghai'})
('name:', 'Frank', 'age:', '37', 'other:', {'gender': 'M', 'job': 'Engineer'})

def enroll(*args, age=6, city='Beijing'):
print args
print 'age :',age
print 'city :',city
enroll("xiaoming", "male")


File "2.py", line 1
def enroll(*args, age=6, city='Beijing'):
^
SyntaxError: invalid syntax

def enroll(name, sex, age=6, city='Beijing'):
print name
print sex
print 'age :',age
print 'city :',city
enroll("xiaoming", "male")


xiaoming
male
age : 6
city : Beijing

上一篇下一篇

猜你喜欢

热点阅读