Python Learning note __4.1 Zhang Gao function

Source: Internet
Author: User

# This is a learning note for the Liaoche teacher Python tutorial

1 , overview

We know that Python's built-in absolute value function is aBS ()

# Call the ABS () function to get a value

>>> ABS (-10)

10

# So, write only ABS itself?

>>> ABS

<built-in function abs>

Visible,ABS (-10) is a function call, and ABS is the function itself

1.1 , assigning a function to a variable

There are only two cases of assigning a function to a variable:

    • The calculation of the function Results assigning to variables

F=abs (-10)

    • Assigning the function itself to a variable, the variable can call the function at this point , that is, the variable can point to the function

F=abs

1.2 , the function name is also a variable

a function name is actually a variable that points to a function .

For ABS (), the function name ABS can be regarded as a variable. This means that you can assign other values to the ABS variable

ABS = 10

After the assignment. Call the ABS function again error

Note: As the ABS function is actually defined in the import builtins module, so to make changes to the ABS variable point in the other modules also take effect, to use the import builtins; builtins.abs =

2 , examples

A simple high-order function that calculates ABS (x) +abs (y)

#-*-Coding:utf-8-*-

def add (x, Y, f):

return f (x) + f (Y)

Print (Add ( -5, 6, ABS)) # the abs function is passed as an argument to the add function


Python Learning note __4.1 Zhang Gao function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.