Python Learning--parameter classification of methods

Source: Internet
Author: User

There are four main parameters for defining methods in Python:

1. General Parameters:

Common parameters are very common.

def F1 (name, age):     Print('My name is%s, I am%s yearsold' % (name, age))

Name and age are two common parameters. When calling method F1, be sure to pass in the name and age two arguments in the order in which they were defined.

F1 ('Andy', 21)

2. Default parameters:

The default parameter refers to a default value in the definition method that is given to a formal parameter.

def F2 (name, age, Home='Shanghai'):    print(' My name is%s, I am%s years old, I come from%s ' % (name, age, Home))

When the method is called, if no arguments are passed, the argument is the value specified when the method is defined, and if the argument is passed at the time of the call, the value of the parameter is the value passed.

F2 ('an', 21)
>>>my name is a, I am years old, I come from Shanghai
F2 (' an ', +, ' Jiangxi ')
>>>my name is a, I am years old, I come from Jiangxi

When defining this method, be sure to place the parameter with the default value at the end of the parameter list.

3. Specifying parameters

The definition of a parameter method is the same as a normal method, and it is not necessary to pass the arguments in the order defined, but to specify which parameter to pass the argument to, in the form of the following:

F1 (age=21, name='an')
>>>my name is a, I am years old

4. Variable parameters

The meaning of a mutable parameter is that there is no limit to the number of arguments passed, and 0 to n arguments are allowed. It is in the form of:

Def calc (*args):
s = 0
For i in args:
S +=i
Print (s)

So we can pass in 0 or more arguments at the time of the call, and these parameters are assembled as a tuple when the function is called.

Calc (1, 2, 3)

What if the data you get beforehand is a list or a tuple? We can add the * number to the list or tuple's arguments before converting them to variable parameters:

num = (3,4,5) calc (*num)
NUM2 = [4,5,7]
Calc (*num2)

5. Keyword parameters:

The parameters of the keyword are preceded by two * numbers, which can be passed in 0 or more parameters that are assembled into a dict when the function is called.

In the example we can just pass the required function name and age, and the printed Kwargs is an empty dict.

def Student (name, age, ** Kwargs)    :print('name:', name, ' Age : ' ' Other : ' , Kwargs) student (' Xiaomi ', 15)
>>>name: Xiaomi age:15 other: {}

Pass multiple keyword arguments

Student (' Xiaomi ', ' city='Beijing', grade=3and other: {  'City'Beijing'grade' : 3}

If there is a dict in advance, we can pass the arguments similar to the value of the variable parameter:

KW = {'city''Beijing'grade': 3}
Student (' Millet ', **kw)

6. Combination Parameters

The combination parameter is a different combination of the above 5 forms, the order of the definition is: Required parameter, default parameter, variable parameter and keyword parameter.

Summary:

When using default parameters, the parameters must be used with immutable objects, and an error will occur with mutable parameters.

*args a tuple is passed in.

**kw passed in is a dict.

Python Learning--parameter classification of methods

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.