• 关键字参数 和 命名关键字参数
# -*- coding: utf-8 -*-

def print_scores(**kw):
    print('         Name    Score')
    print('-----------------------')
    for name, score in kw.items():
        print('   %10s      %d' % (name, score))
    print()

print(print_scores(Adam=99, Lisa=88, Bart=77))

data = {
    'Adam Lee': 99,
    'Lisa S': 88,
    'F.Bart': 77,
}

print_scores(**data)

def print_info(name, *, gender, city='Beijing', age):
    print('Personal Info')
    print('---------------')
    print('   Name: %s' % name)
    print(' Gender: %s' % gender)
    print('   City: %s' % city)
    print('    Age: %s' % age)
    print()

print_info('Bob', gender='male', age=20)
print_info('Lisa', gender='female', city='Shanghai', age=18)

 

相关文章:

  • 2022-12-23
  • 2021-11-14
  • 2021-11-30
  • 2021-12-19
  • 2021-12-14
  • 2021-04-10
  • 2021-06-26
  • 2021-06-01
猜你喜欢
  • 2022-12-23
  • 2021-10-16
  • 2021-10-18
  • 2021-10-28
  • 2021-10-02
  • 2022-12-23
  • 2021-09-14
相关资源
相似解决方案