[Timlinux] Python functions

Source: Internet
Author: User

1. The difference between function and method

Methods: Blocks of code declared in a class structure by DEF statements are called methods, such as class methods (Classmethod), instance methods, static methods (Staticmethod), and so on.

Functions: Blocks of code declared by DEF statements in other scenarios are called functions, such as built-in functions, global functions, and so on.

2. Definition of function 2.1. anonymous functions

Func_name1 = Lambda arg1, arg2:arg1 + arg2
Print (FUNC_NAME1)

2.2. No parameter function

Def func_name2 ():

Print ("in Func_name2")

2.3. Positional parameter functions

def func_name3 (Arg1, arg2):

x = arg1 + arg2

Print (arg1, "+", arg2, "=", x)

2.4. Variable-length parametric functions

def func_name4 (*args):

For Arg in args:

Print (ARG)

Func_name4 (output:1) #, 2, 3

Func_name4 (*[4,5,6]) # Output:4, 5, 6

Func_name4 (* (7,8,9)) # Output:7, 8, 9

A = [A]
Func_name4 (a) # output: [[+]--
Func_name4 (*a) # output:1--, 2--, 3--

2.5. Keyword parameter function

def func_name5 (name, age):

Print (name, age)

Func_name5 (' Tim ', 22)

Func_name5 (' Tim ', age=22)

Func_name5 (name= ' Tim ', age=100)

2.6. Variable-length keyword parameter function

def func_name6 (**kwargs):

For K in Kwargs:

Print (k, kwargs[k])

Func_name6 (name= ' Tim ', age=100)

Func_name6 (name= ' Tim ', age=100, sex= "Male")

3. Function closure

<T.B.D.>

[Timlinux] Python functions

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.