Home Backend Development Python Tutorial How to set the default parameters of Python functions

How to set the default parameters of Python functions

May 04, 2023 pm 07:01 PM
python

We know that if a parameter is not specified when calling a function, the Python interpreter will throw an exception. In order to solve this problem, Python allows setting default values ​​​​for parameters, that is, when defining a function, directly specify a default value for the formal parameters. In this case, even if no parameter is passed to a formal parameter with a default value when the function is called, the parameter can directly use the default value set when the function is defined.

Python defines a function with default value parameters. The syntax format is as follows:

def 函数名(...,形参名,形参名=默认值):
    代码块
Copy after login

Note that when using this format to define a function, the formal parameters with default values ​​must be specified in all default values. value parameter at the end, otherwise a syntax error will occur.

The following program demonstrates how to define and call a function with default parameters:

#str1没有默认参数,str2有默认参数
def dis_str(str1,str2 = "http://c.biancheng.net/python/"):
print("str1:",str1)
print("str2:",str2)
 
dis_str("http://c.biancheng.net/shell/")
dis_str("http://c.biancheng.net/java/","http://c.biancheng.net/golang/")
Copy after login

The running result is:

str1: http://c.biancheng. net/shell/
str2: http://c.biancheng.net/python/
str1: http://c.biancheng.net/java/
str2: http://c.biancheng .net/golang/

In the above program, the dis_str() function has 2 parameters, the second of which has a default parameter. This means that when calling the dis_str() function, we can pass in only 1 parameter, which will be passed to the str1 parameter, and str2 will use the default parameters, as shown in line 6 of the program.

Of course, when calling the dis_str() function, you can also pass values ​​to all parameters (as shown in line 7 of the code). At this time, even if str2 has a default value, it will give priority to the parameters passed to it. new value.
At the same time, combined with keyword parameters, the following three ways of calling the dis_str() function are also possible:

dis_str(str1 = "http://c.biancheng.net/shell/")
dis_str("http://c.biancheng.net/java/",str2 = "http://c.biancheng.net/golang/")
dis_str(str1 = "http://c.biancheng.net/java/",str2 = "http://c.biancheng.net/golang/")
Copy after login

Again, when defining a function with default value parameters, there are parameters with default values. Must come after all parameters without default values. Therefore, the function defined in the following example is incorrect:

#语法错误
def dis_str(str1="http://c.biancheng.net/python/",str2,str3):
pass
Copy after login

Obviously, str1 has a default value, while str2 and str3 do not have default values, so str1 must be located after str2 and str3.

Some readers may ask, for your own custom functions, you can easily know which parameters have default values, but if you use the built-in functions provided by Python, or functions provided by other third parties, how do you know which parameters? Is there a default value?

In Pyhton, you can use "function name.__defaults__" to view the current value of the function's default value parameter, and its return value is a tuple. Take the dis_str() function in this section as an example. Based on it, execute the following code:

print(dis_str.__defaults__)
Copy after login

The program execution result is:

('http://c. biancheng.net/python/',)

The above is the detailed content of How to set the default parameters of Python functions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

A complete guide to golang function debugging and analysis

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

See all articles