Python parameter Type __python

Source: Internet
Author: User

Python functions have 5 types of parameters:

Positional parameters: Also called the required parameters, the definition of the function parameters are given in the order of location, the call must be given a value, and must be placed at the front

Default parameter: Parameter has default value, can not be given when called, is handled by default

Note: The default parameter must point to an immutable object.

Variable parameters: This parameter can be defined when there is no certainty as to how much input, *args, if the argument has been formed in a list or tuple, precede it with *

Keyword parameters: Define **kwargs, call can pass any unrestricted arguments, but the parameter name and parameter values are passed in together, if the dictionary has been formed, precede it with * * Incoming can be

Named keyword parameter: one of the restrictions on keyword parameters that only accepts a fixed named keyword parameter, defined as after a variable parameter. If you do not have a variable parameter, you need to precede it with a * number such as: Def Eat (food,juice,time=18,*,place,people), which must be passed in with the parameter name and the value of the argument. can give the default value. There is no order between named keyword parameters.

Define time Order: Positional parameters, default parameters, variable parameters, named keyword parameters, keyword parameters. When called, the default parameter must be copied and cannot be omitted if there is a variable parameter. Otherwise, the value of the variable parameter is paid to the default parameter.


#python函数参数
Import PDB
def info (name,age,country= "lang=", "Chinese", *args,city= "Beijing", Sex,**kwargs):
Print ("Positional and Default parameters:")
Print (' My name is {p1}and I am {p2} years old,i come ' {p3},my mother language ' is {P4} '. Format (p1=name,p2=age,p3=countr Y,p4=lang))
Print ("named keyword parameter:")
Print (City,sex)
Try
if bool (args):
Print ("Variable parameters:")
Print (args) #元组类型
Print (*args) #元组中的参数
if BOOL (Kwargs):
Print ("Keyword parameters:")
Print (Kwargs)
Print (*kwargs) #只能打印出键
Except
Print (type (args))
Print ("\ n")
T1 = (' Chifan ', ' Shuijiao ', ' Dadoudou ')
Dict1 = {' Hobby ': ' Sleep ', ' Shengao ': 198}
Print ("Call Mode 1: Parameters that must be given a value")
Info ("Lilei", 18,sex= "man") #位置参数和命名关键字必须传值
Print ("Call Mode 2: No order between named keyword parameters")
Info ("Lilei", 18,sex= "man", City= "Shanghai") #命名关键字参数之间无顺序
Print ("Call Mode 3: Call all values in order")
Info ("Hali", "England", "中文版", "haha", "Zhazha", "Chacha", "Houhou", sex= "woman", **dict1)
Info ("Hali", "England", "中文版", *t1,sex= "woman", **dict1)
Info ("Hali", 34,country= "France", lang= "Franch", sex= "man", **dict1)

#info ("Hali", 34,country= "France", lang= "Franch", *t1,sex= "man", **dict1) #此种复制方法会出错, interpreter error Typeerror:info () got Multiple values for argument ' country '


Results:

Call Mode 1: Parameters that must be given a value
Positional parameters and Default parameters:
My name are Lileiand I am years old,i come from china,my mother language are Chinese
Named keyword parameter:
Beijing Mans




Call Mode 2: No order between named keyword parameters
Positional parameters and Default parameters:
My name are Lileiand I am years old,i come from china,my mother language are Chinese
Named keyword parameter:
Shanghai Mans




Call Mode 3: Call all values in order
Positional parameters and Default parameters:
My name are Haliand I am years old,i come from england,my mother language is 中文版
Named keyword parameter:
Beijing woman
Variable parameters:
(' haha ', ' Zhazha ', ' chacha ', ' Houhou ')
haha Zhazha chacha Houhou
Keyword parameters:
{' Hobby ': ' Sleep ', ' Shengao ': 198}
Hobby Shengao




Positional parameters and Default parameters:
My name are Haliand I am years old,i come from england,my mother language is 中文版
Named keyword parameter:
Beijing woman
Variable parameters:
(' Chifan ', ' Shuijiao ', ' Dadoudou ')
Chifan Shuijiao Dadoudou
Keyword parameters:
{' Hobby ': ' Sleep ', ' Shengao ': 198}
Hobby Shengao




Positional parameters and Default parameters:
My name are Haliand I am years old,i come from france,my mother language are Franch
Named keyword parameter:
Beijing Mans
Keyword parameters:
{' Hobby ': ' Sleep ', ' Shengao ': 198}
Hobby Shengao

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.