Python-100-Days icon indicating copy to clipboard operation
Python-100-Days copied to clipboard

函数调用加不加括号的区别

Open andriywh opened this issue 5 years ago • 3 comments

DAY9的案例2:发牌游戏

def main(): p = Poker() p.shuffle() players = [Player('东邪'), Player('西毒'), Player('南帝'), Player('北丐')] for _ in range(13): for player in players: player.get(p.next) for player in players: print(player.name + ':', end=' ') player.arrange(get_key) print(player.cards_on_hand)

为什么get_key函数调用时不用加括号?

andriywh avatar Nov 28 '19 05:11 andriywh

是否是因为这里的get_key作为参数传入,而不是正常调用该函数?

[email protected]

发件人: andriywh 发送时间: 2019-11-28 13:47 收件人: jackfrued/Python-100-Days 抄送: Subscribed 主题: [jackfrued/Python-100-Days] 函数调用加不加括号的区别 (#382) DAY9的案例2:发牌游戏 def main(): p = Poker() p.shuffle() players = [Player('东邪'), Player('西毒'), Player('南帝'), Player('北丐')] for _ in range(13): for player in players: player.get(p.next) for player in players: print(player.name + ':', end=' ') player.arrange(get_key) print(player.cards_on_hand) 为什么get_key函数调用时不用加括号? — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

Satday avatar Nov 29 '19 07:11 Satday

是否是因为这里的get_key作为参数传入,而不是正常调用该函数? [email protected] 发件人: andriywh 发送时间: 2019-11-28 13:47 收件人: jackfrued/Python-100-Days 抄送: Subscribed 主题: [jackfrued/Python-100-Days] 函数调用加不加括号的区别 (#382) DAY9的案例2:发牌游戏 def main(): p = Poker() p.shuffle() players = [Player('东邪'), Player('西毒'), Player('南帝'), Player('北丐')] for _ in range(13): for player in players: player.get(p.next) for player in players: print(player.name + ':', end=' ') player.arrange(get_key) print(player.cards_on_hand) 为什么get_key函数调用时不用加括号? — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

请问能详细说一下 函数作为参数传入的机制吗?

andriywh avatar Dec 02 '19 00:12 andriywh

是否是因为这里的get_key作为参数传入,而不是正常调用该函数? [email protected] 发件人: andriywh 发送时间: 2019-11-28 13:47 收件人: jackfrued/Python-100-Days 抄送: Subscribed 主题: [jackfrued/Python-100-Days] 函数调用加不加括号的区别 (#382) DAY9的案例2:发牌游戏 def main(): p = Poker() p.shuffle() players = [Player('东邪'), Player('西毒'), Player('南帝'), Player('北丐')] for _ in range(13): for player in players: player.get(p.next) for player in players: print(player.name + ':', end=' ') player.arrange(get_key) print(player.cards_on_hand) 为什么get_key函数调用时不用加括号? — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

请问能详细说一下 函数作为参数传入的机制吗?

对于 sort 排序方法,传递给key参数的是一个形参函数,它指定可迭代对象中的每一个元素来按照该函数进行排序,作为形参函数调用,并不需要去加括号(即传递的是 get_key 函数本身,return 的两个参数作为对该数组的排序依据)

CrookedNeck avatar Feb 23 '23 10:02 CrookedNeck