Python function Introduction 3: python function Introduction

Source: Internet
Author: User

Python function Introduction 3: python function Introduction

1. Non-fixed number of arguments-parameter group, * args. The result is displayed in the form of tuples.

Def func (* args): print (args) func (, 5) # first call method, multiple real parameters func (* [, 5]) # The second method is called in the form * [].
# Running result (1, 2, 3, 4, 5) (1, 2, 3, 4, 5)

2. Fixed Number of real parameters and parameter groups. Combination of the two parameters (a, * args)

Def func2 (x, * args): print (x, args) func2 ('A', 1, 2, 3, 4) # deduct the fixed parameters. All other real parameters are included in the parameter group, list in the form of tuples # result a (1, 2, 3, 4)

3. ** kwargs converts a real parameter (key = value) into a dictionary.

Def func3 (** kwargs): print (kwargs) print (kwargs ['name']) print (kwargs ['age']) func3 (name = 'frank ', age = 26) # result: {'name': 'frank', 'age': 26} Frank26

4. args, * args, ** kwargs

Def func5 (name, * args, ** kwargs): print (name, args, kwargs) func5 ('Alex ', 1, 3, 'teacher', age = 18, sex = 'M') # result Alex (1, 3, 'teacher') {'age': 18, 'sex': 'M '}

5. High-order functions: a variable can point to a function. If a function parameter can accept a variable, one function can take another function as a parameter.

Def func (a, B, f): return f (a) + f (B) B = func (1,-1, abs) print (B) # result 2

 

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.