Define Functions: Keyword Arguments

Functions can also be called using keyword arguments of the form kwarg=value. For instance, the following function:

def parrot(voltage, state=‘a stiff‘, action=‘voom‘, type=‘Norwegian Blue‘):
    print("-- This parrot wouldn‘t", action, end=‘ ‘)
    print("if you put", voltage, "volts through it.")
    print("-- Lovely plumage, the", type)
    print("-- It‘s", state, "!")

accepts one required argument (voltage) and three optional arguments (state, action, and type). This function can be called in any of the following ways:

parrot(1000)                                          # 1 positional argument
parrot(voltage=1000)                                  # 1 keyword argument
parrot(voltage=1000000, action=‘VOOOOOM‘)             # 2 keyword arguments
parrot(action=‘VOOOOOM‘, voltage=1000000)             # 2 keyword arguments
parrot(‘a million‘, ‘bereft of life‘, ‘jump‘)         # 3 positional arguments
parrot(‘a thousand‘, state=‘pushing up the daisies‘)  # 1 positional, 1 keyword

but all the following calls would be invalid:

parrot()                     # required argument missing
parrot(voltage=5.0, ‘dead‘)  # non-keyword argument after a keyword argument
parrot(110, voltage=220)     # duplicate value for the same argument
parrot(actor=‘John Cleese‘)  # unknown keyword argument

In a function call, keyword arguments must follow positional arguments. All the keyword arguments passed must match one of the arguments accepted by the function (e.g. actor is not a valid argument for the parrot function), and their order is not important.

1  关键字参数得在位置参数后面;

2  关键字参数的顺序可以是任意的;

时间: 2024-10-03 06:42:00

Define Functions: Keyword Arguments的相关文章

Note of Define Functions

The default values are evaluated at the point of function definition in the defining scope, so that i = 5 def f(arg=i): print(arg) i = 6 f() will print 5. Important warning: The default value is evaluated only once. This makes a difference when the d

[TypeScript] Define Custom Type Guard Functions in TypeScript

One aspect of control flow based type analysis is that the TypeScript compiler narrows the type of a variable within a type guard. This lesson explores how you can define functions and type predicates to create your own type guards similar to the Arr

python3.4 build in functions from 官方文档 翻译中

2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.     Built-in Funct

First-Class Functions

The "First-Class object" in Python: Created at runtime Assigned to a variable or element in a data structure Pass as an augument to a function Return as the result of a function Integers, strings and dictionaries are other examples of first-clas

Understand JavaScript Callback Functions and Use Them

In JavaScript, functions are first-class objects; that is, functions are of the type Object and they can be used in a first-class manner like any other object (String, Array, Number, etc.) since they are in fact objects themselves. They can be “store

Javascript—Higher Order Functions

Higher order functions are functions that manipulate other functions. For example, a function can take other functions as arguments and/or produce a function as its return value. Such fancy functional techniques are powerful constructs available to y

1 Building Abstractions with Functions

Every powerful language has three such mechanisms: primitive expressions and statements, which represent the simplest building blocks that the language provides, means of combination, by which compound elements are built from simpler ones, and means

Flask学习笔记——配置管理

1.硬编码: app = Flask(__name__) # app.config 是flask.config.Config类的实例,继承自内置数据结构dict app.config['DEBUG'] = True 2.参考--flask.config.Config类: class Config(dict): """Works exactly like a dict but provides ways to fill it from files or special dict

python学习之函数

1.函数名可以被赋值 比如: def aaa(): pass b = aaa//将函数名字赋值给b b()//跟aaa()效果一样 2.return 2.1.如果函数不写return的话,会默认返回None 2.2.return后,函数下面的语句不会被执行,中断函数操作 2.3.return个什么东西都行,哪怕是个列表..... 3.pycharm使用断点调试的话,需要用debug模式(向右小箭头的小虫子) 4.参数: 默认参数必须写在后边 def aaa(a1, a2 = 1): pass//