我想创建带有两个默认参数的函数,另一个默认参数为* args和** kwargs
我具有使用参数和关键字的功能。如果在函数中我设置了前两个值,则一切正常,但我希望此参数为默认值。 我该怎么办?
def the_credits(length_of_line=80, empty_space_between_lines=0, *args, **kwargs):
for arg in args:
for a in str(arg).center(length_of_line // 2):
print(a, end='')
if a != " ":
time.sleep(0.4)
print('\n' * empty_space_between_lines)
for kwarg in kwargs:
for k in (kwarg + ' : ' + kwargs[kwarg]).center(length_of_line // 2):
print(k, end='')
if k != " ":
time.sleep(0.4)
print('\n' * empty_space_between_lines)
input()
the_credits("Cudny Program Pokazowy",
"Zemsta zlego markiza Cul-de-Sac",
Scenariusz="Autor",
Scenografia="Autor",
Kamerowanie="Brak",
Kostiumy="Natura",
Podziekowana="Pyton")
此示例有效
the_credits(80, 0, "Cudny Program Pokazowy",
"Zemsta zlego markiza Cul-de-Sac",
Scenariusz="Autor",
Scenografia="Autor",
Kamerowanie="Brak",
Kostiumy="Natura",
Podziekowana="Pyton")
答案 0 :(得分:2)
您可以只使用kwargs
和args
并按名称指定这些有问题的参数;如果length_of_line
或empty_space_between_lines
中没有kwargs
,则您知道使用默认值。