惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
月光博客
月光博客
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
Recent Announcements
Recent Announcements
MyScale Blog
MyScale Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
F
Fortinet All Blogs
小众软件
小众软件
D
Docker
U
Unit 42
博客园 - 聂微东
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
有赞技术团队
有赞技术团队
腾讯CDC

博客园 - 芭蕉

Model-View-ViewModel(MVVM) with MEF 使用EnvDTE自动格式整个工程 Inversion of Control Duck Typing in C# - 芭蕉 Dynamic Proxy C# 4.0 Dynamic --通过DLR直接调用IronPython 介绍一个Python进行函数式编程时有用的module - 芭蕉 - 博客园 lambda in F# - 芭蕉 UML类关系图标识速记 VisualStudio quick tips -- 快速在多个打开的代码文件间切换 VisualStudio quick tips --快速调整字体大小 结合实例实习F#(三)--理解函数式语言中的函数 VisualStudio quick tips --快速打开Properties Page 结合实例学习F#(二) --基本数据类型Discriminated Unions 结合实例学习F#(一) --快速入门 Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance Factory Method 顺时钟方向螺旋状打印矩阵元素 玩转Visual Studio ---Debug篇
Python functions
芭蕉 · 2009-12-24 · via 博客园 - 芭蕉

Python中函数支持default value和keyword arguments(类似于C# 4.0中引入的named and optional parameters) . 唯一需要注意的地方就是在一个scope中默认值只会被计算一次,所以如果默认值是可变容器时,要注意side effects.

比如

def f(a, L=[]):

  L.append(a)

  return L

f(1) // return [1]

f(2) // return [1,2]

在Python中我们也能常见到这样的函数定义 def f(a, *args,**keyargs), **keyargs是Dictionary类型,对应于*args除外的所有keyword arguments, *args是Tuple类型,对应于所有普通参数除外的positional parameter.

比如 f(1,'1','2', para_name=1)  //a =1, args = ('1','2'), keyargs ={'para_name':1}

忘了一点,如果在函数内有object赋值,刚该object被自动视为local object,如果需要的是global object,需显式申明该object为global object.

c = 1

def f():

  c  = c+1 // UnboundLocalError here

需要改为:

c=1

def f():

  global c

  c = c +1