jackfrued / Python-100-Days

Python - 100天从新手到大师
155.45k stars 52.12k forks source link

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

Open andriywh opened 4 years ago

andriywh commented 4 years ago

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函数调用时不用加括号?

Satday commented 4 years ago

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

357897077@qq.com

发件人: 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 commented 4 years ago

是否是因为这里的getkey作为参数传入,而不是正常调用该函数? 357897077@qq.com 发件人: 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.

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

CrookedNeck commented 1 year ago

是否是因为这里的getkey作为参数传入,而不是正常调用该函数? 357897077@qq.com 发件人: 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 的两个参数作为对该数组的排序依据)