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 default is a mutable object such as a list, dictionary, or instances of most classes. For example, the following function accumulates 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))

This will print

[1]
[1, 2]
[1, 2, 3]

If you don’t want the default to be shared between subsequent calls, you can write the function like this instead:

def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return L
时间: 2024-08-05 16:57:14

Note of Define Functions的相关文章

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=' ') pri

[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

Using Single-Row Functions to Customize Output使用单行函数自定义输出

DUAL is a public table that you can use to view results from functions and calculations. SQL> select * from DUAL; D - X SQL> desc DUAL; Name                                      Null?    Type ----------------------------------------- -------- ------

android从应用到驱动之—camera(2)---cameraHAL的实现

本来想用这一篇博客把cameraHAL的实现和流程都给写完的.搞了半天,东西实在是太多了.这篇先写cameraHAL的基本实现框架,下一篇在具体写camerahal的流程吧. cameraHAL的实现: 对于初学者来说,最大的疑问是系统是如何调用hardware的.这里就以camera来举例说明.调用hardware的程序是cameraservice,我们就去它里面看看它是如何找到hardware的 先把源码贴上来: /* ** ** Copyright (C) 2008, The Androi

android从应用到驱动之—camera(2)---cameraHAL的实现(转)

cameraHAL的实现: 对于初学者来说,最大的疑问是系统是如何调用hardware的.这里就以camera来举例说明.调用hardware的程序是cameraservice,我们就去它里面看看它是如何找到hardware的 先把源码贴上来: /* ** ** Copyright (C) 2008, The Android Open Source Project ** ** Licensed under the Apache License, Version 2.0 (the "License

underscore-1.8.3-analysis.js

1 // Underscore.js 1.8.3 2 // http://underscorejs.org 3 // (c) 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors 4 // Underscore may be freely distributed under the MIT license. 5 // 中文注释 by hanzichi @https://github.com/h

[New Learn] RunLoop学习-官方译文

Run Loops Run loops是线程的一个基本构成部分.一个run loop 是一个事件处理循环,你可以使用它来处理线程收到的事件.设计run loop的目的就是可以使得线程在收到事件的时候处理事件而为收到事件的时候进入sleep状态(个人理解,线程如果启用了run loop那么这个线程就变成了一个内部的服务线程,不会主动死掉) 对于Run loop 的管理并不是全部系统自动完成的.你必须自己完成线程中run loop的启动并编写逻辑代码去处理收到的时间.在Cocoa和 Core Fou

JAVASCRIPT的一些知识点梳理

春节闲点,可以安心的梳理一下以前不是很清楚的东东.. 看的是以下几个URL: http://web.jobbole.com/82520/ http://blog.csdn.net/luoweifu/article/details/41466537 http://javascriptissexy.com/understand-javascript-closures-with-ease/ http://javascriptissexy.com/javascript-variable-scope-an