Home > Backend Development > Python Tutorial > Usage and key points to note about the default values ​​of Python function parameters

Usage and key points to note about the default values ​​of Python function parameters

不言
Release: 2019-04-15 11:07:29
forward
4059 people have browsed it

This article brings you the usage and key points to note about the default values ​​of python function parameters. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

The most useful form is to specify a default value for one or more parameters. The function thus created can be called with fewer arguments than allowed when defined, for example:

def ask_ok(prompt, retries=4, reminder='Please try again!'):
    while True:
        ok = input(prompt)
        if ok in ('y', 'ye', 'yes'):
            return True
        if ok in ('n', 'no', 'nop', 'nope'):
            return False
        retries = retries - 1
        if retries < 0:
            raise ValueError(&#39;invalid user response&#39;)
        print(reminder)
Copy after login

This function can be called in several ways:

Give only the required Parameters: <span class="pre">ask_ok('Do <span class="pre">you <span class="pre">really <span class="pre">want <span class="pre">to <span class="pre">quit?')<br/></span></span></span></span> </span></span> Give an optional parameter: <span class="pre">ask_ok('OK <span class="pre">to <span class="pre">overwrite <span class="pre">the <span class="pre">file?', <span class="pre">2 )<br/></span></span></span></span></span></span>Or give all parameters: <span class="pre">ask_ok('OK <span class="pre">to <span class="pre">overwrite <span class="pre">the <span class="pre">file?', <span class="pre">2, <span class="pre">'Come <span class="pre">on, <span class="pre">only <span class="pre">yes <span class="pre">or <span class="pre">no!')</span></span></span></span></span></span></span></span></span></span></span></span>

#This example also introduces

in<span class="pre"></span> keyword. It can test whether a sequence contains a certain value.

The default value is calculated at the function definition in the

definition process, so

i = 5

def f(arg=i):
    print(arg)

i = 6
f()
Copy after login

will print 5.

Important warning: The default value will only be executed once. This rule is important when the default value is a mutable object (Python lists, dictionaries, and most class instances). For example, the following function will store the arguments passed to it on subsequent calls:

def f(a, L=[]):
    L.append(a)
    return L

print(f(1))
print(f(2))
print(f(3))
Copy after login

This will print

[1]
[1, 2]
[1, 2, 3]
Copy after login

If you don't want the default value to be shared between subsequent calls, you can Write this python function like this:

def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return L
Copy after login

The above is the detailed content of Usage and key points to note about the default values ​​of Python function parameters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template