The first class object of a function name is just a name, relative to the second class object. Most of the functions we’ve used so far are first class objects.

Objects of the first class of function names have four main applications:

  1. A function name can be assigned to a variable as a value
  2. The function name can be used as an argument to another function
  3. The function name can be used as the return value of another function
  4. Function names can be placed in containers as elements

Example 1:

def func() :
    print(1)

a = func
print(func)  The memory address of the function
print(a)
a()
Copy the code

The output is:

<function func at 0x00000197E2D41EA0>
<function func at 0x00000197E2D41EA0>
1
Copy the code

Both func and a refer to the memory address of the function. When we call a, we get the same result as when we call func.

Example 2:

def func() :
    print(1)
def foo(a) :
    print(a)
foo(func)
Copy the code

The output is:

<function func at 0x0000021FD82C1EA0>
Copy the code

Example 3:

def func() :
    return 1
def foo(a) :
    return a
cc = foo(func)
print(cc)
Copy the code

The output is:

<function func at 0x000002261A231EA0>
Copy the code

Example 4:

There is such a requirement: the user selects the corresponding sequence number to select the next operation, for example, select 1 will call the registration function, select 2 will call the login function… Previously we would have done this through a process control statement:

"Python Learning exchange, free public lecture, free q&A, system learning group: 579817333"
def login() :
    print("Login")

def register() :
    print("Registered")

def shopping() :
    print("Go")

def add_shopping_car() :
    print("Add")

def buy_goods() :
    print("Buy")

msg =Register 2. log in 3. browse 4. add 5. buy Please enter the serial number you want to choose: ""
while True:
    choose = input(msg)
    if choose.isdecimal():
        if choose == "1":
            register()
        elif choose == "2":
            login()
        elif choose == "3":
            shopping()
        elif choose == "4":
            add_shopping_car()
        elif choose == "5":
            buy_goods()
        else:
            print("Please enter the correct serial number")
Copy the code

By packing functions into dictionaries, we can drastically reduce the amount of duplicate code:

def login() :
    print("Login")

def register() :
    print("Registered")

def shopping() :
    print("Go")

def add_shopping_car() :
    print("Add")

def buy_goods() :
    print("Buy")

msg =Register 2. log in 3. browse 4. add 5. buy Please enter the serial number you want to choose: ""
func_dic = {"1":register,
            "2":login,
            "3":shopping,
            "4":add_shopping_car,
            "5":buy_goods}
while True:
    choose = input(msg) # "5"
    if choose in func_dic:
        func_dic[choose]()
    else:
        print("Please enter the correct serial number")
Copy the code

This way of using dictionaries to call functions is an important programming idea that will be used a lot in the future.