简体   繁体   English

如何在字典中使用字典调用函数

[英]How calling a function using dictionary works in python

In order to call a function using dictionary we use the following code: 为了使用字典调用函数,我们使用以下代码:

def f1():
    print ("One")

def f2():
    print ("Two")

def f3():
    print ("Three")

dict = {1: f1, 2: f2, 3: f3}

dict[1]()

How does the last line dict[1]() actually works? 最后一行dict[1]()实际如何工作?

Functions in Python are first class objects. Python中的函数是一流的对象。 This means that functions are objects that can be sent as parameters, they can be stored in data structures and you can do with them same as simple objects. 这意味着函数是可以作为参数发送的对象,可以将它们存储在数据结构中,并且可以像简单对象一样使用它们。 You can read about them here . 您可以在这里阅读有关它们的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM