Parameter transfer of Python learning

Source: Internet
Author: User

^ parameter passing is divided into two cases: definition (formal parameter) and call (actual argument). ^

1. Definition (formal parameters)
    • Default parameters
def func(x, y=None):# 任何时候必须  优先定义   位置参数# 默认参数和可变参数*args  顺序无要求# 任何时候必须  最后定义   可变参数**kwargs
    • Tuple of variable parameters
def func(a, x=None, *y):# x为默认参数;# y为元组,可以为空,可以有1到多个元素,函数将依次读取y的元素,然后再函数里组成一个元组。func(1)          # 1, None, ()func(1,())       # 1, (),   ()func(1,2,3,8)      # 1, 2,  (3,8)
    • Dict of variable parameters
def func(x, y=0, **z):# x为位置参数# y为字典,可以为空,可以有1到多个字典元素,函数依次读取每个键值对,并传入在函数里组成一个字典。func(1)          # 1, 0, {}func(1, y=2, w=3)   # 1, 0, {‘y‘: 2, ‘w‘: 3}
2. Call (argument)
def func(a,b,c):tuple = (1, 2, 3)func(*tuple)       # 1,2,3# *tuple 将tuple拆分为三个元素,一次传递给函数的位置参数a,b,c
def func(a,b,c):dict = {‘a‘: 1, ‘b‘: 2, ‘c‘: 3}func(**dict)       # 1,2,3# **dict将dict每个值按照键值对的关系传递给函数的对应参数# 因此key必须和位置参数(默认参数可省略)一一对应,不多不少才能成功

Parameter transfer of Python learning

Related Article

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.