Introduction to parameter passing and variable length parameters of functions in Python

Source: Internet
Author: User
This article mainly introduces the parameter passing and variable length parameters of functions in Python. This article provides multiple code instances to explain a variety of function parameters. For more information, see 1. There are also default functions like C ++ in Python.

The Code is as follows:


Def foo (text, num = 0 ):
Print text, num

Foo ("asd") # asd 0
Foo ("def", 100) # def 100

When defining a function with default parameters, these default parameters must be placed behind non-default parameters.

When the default value is provided, the provided value is used. Otherwise, the default value is used.

2. Python can pass parameters according to the parameter name

The Code is as follows:


Def foo (ip, port ):
Print "% s: % d" % (ip, port)

Foo ("192.168.1.0", 3306) #192.168.1.0: 3306
Foo (port = 8080, ip = "127.0.0.1") #127.0.0.1: 8080


Row 3, with no parameter name specified, transmits parameters in sequence.

Line 2: Specify the parameter name. You can pass the parameter by parameter name.

3. Variable Length Parameters

The Code is as follows:


# Coding: UTF-8 # Set the python file encoding to UTF-8, so that you can write Chinese comments.
Def foo (arg1, * tupleArg, ** dictArg ):
Print "arg1 =", arg1 # formal_args
Print "tupleArg =", tupleArg #()
Print "dictArg =", dictArg # []
Foo ("formal_args ")


The parameter in the above function, "*" before tupleArg indicates that this parameter is a tuples parameter. From the output of the program, we can see that the default value is (); "**" in front of dicrtArg indicates this dictionary parameter (key-Value Pair parameter ). TupleArg and dictArg can be considered as two default parameters. Redundant non-Keyword parameters are placed in the tupleArg parameter of the tuples during function calls. Redundant keyword parameters are placed in the dictionary parameter dictArg.

The following are some usage of variable length parameters:

The Code is as follows:


# Coding: UTF-8 # Set the python file encoding to UTF-8, so that you can write Chinese comments.
Def foo (arg1, arg2 = "OK", * tupleArg, ** dictArg ):
Print "arg1 =", arg1
Print "arg2 =", arg2
For I, element in enumerate (tupleArg ):
Print "tupleArg % d --> % s" % (I, str (element ))
For key in dictArg:
Print "dictArg % s --> % s" % (key, dictArg [key])

MyList = ["my1", "my2"]
MyDict = {"name": "Tom", "age": 22}
Foo ("formal_args", arg2 = "argSecond", a = 1)
Print "*" * 40
Fool (123, myList, myDict)
Print "*" * 40
Foo (123, rt = 123, * myList, ** myDict)

Output:

From the above procedure, we can see that:

(1) line 3 of the Code.

If "*" tuples or "**" dictionary parameters are used in parameters, these two parameters should be placed at the end of the parameter list. The "*" parameter is located before the "**" dictionary parameter.

Keyword parameter rt = 123, because the function foo (arg1, arg2 = "OK", * tupleArg, ** dictArg) does not have the rt parameter, it is also included in the dictionary parameter.

(2) line 3 of the Code.

If "*" is not included in the front of the object, or if "*" is not included in the front of the dictionary object, it is used as a common object to pass parameters.

For redundant common parameters, in foo (123, myList, myDict), 123 is assigned to arg1 and myList to arg2. The redundant parameter myDict is assigned to myList by default.

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.