【发布时间】:2020-11-26 20:15:07
【问题描述】:
你能在 Python 中做这样的事情吗?
def give_me_your_three_favorite_things_in_each_given_category(
food=(first, second, third),
color=(first, second, third))
println(f"Your favorite foods are {food.first}, {food.second} and {food.third}")
println(f"Your favorite colors are {color.first}, {color.second} and {color.third}")
give_me_your_three_favorite_things_in_each_given_category(
food=(first="pizza", second="pasta", third="ice cream"),
color=(first="black", second="blue", third="green")
预期输出:
“你最喜欢的食物是比萨、意大利面和冰淇淋”
"你最喜欢的颜色是黑色、蓝色和绿色"
【问题讨论】:
-
使用字典?
def ...(food, ...): println(f"...{food['first']}...")和give...(food={'first': 'pizza', ...})? -
@deceze 我想要一个指定所需参数数量的方法。您和 maor10 的答案很好,但在您希望此方法的用户确切知道要传递多少个参数以及这些参数的确切含义时,情况并非如此。
标签: python python-3.x methods parameters parameter-passing