## python 中的函式定义 原

lemos

``````>>> def fib(n):    # 打印 Fibonacci 序列到 n
...     """打印到 n 的 Fibonacci 序列."""
...     a, b = 0, 1
...     while a < n:
...         print(a, end=' ')
...         a, b = b, a+b
...     print()
...
>>> # 现在调用我们刚定义的函式:
... fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597``````

``````>>> fib(0)
>>> print(fib(0))
None``````

* 关于符号表

``````import dis

c = 3

def abc():
a = 1
b = a + c

dis.dis(abc)``````

`````` 19           0 LOAD_CONST               1 (1)
3 STORE_FAST               0 (a)

20           6 LOAD_FAST                0 (a)
9 LOAD_GLOBAL              0 (c)
13 STORE_FAST               1 (b)
16 LOAD_CONST               0 (None)
19 RETURN_VALUE``````

### 默认参数

``````def meet(you='lisi', him='wangwu'):
print(you,'meet',him)

meet()
meet('wo')
meet('wo','zhangsan')
``````

``````i = 5

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

i = 6
f()

# output:
5``````

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

print(f(1))
print(f(2))
print(f(3))

# output:
[1]
[1, 2]
[1, 2, 3]``````

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

* 为什么默认参数会变？

``````def foo(bar=None):
bar = bar or []``````

``````def foo(bar=[]):
bar = list(bar)``````

### 关键字参数

``````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, "!")``````

``````parrot(1000)
parrot(action = 'VOOOOOM', voltage = 1000000)
parrot('a thousand', state = 'pushing up the daisies')
parrot('a million', 'bereft of life', 'jump')``````

``````def cheeseshop(kind, *arguments, **keywords):
print("-- Do you have any", kind, "?")
print("-- I'm sorry, we're all out of", kind)
for arg in arguments:
print(arg)
print("-" * 40)
keys = sorted(keywords.keys())
for kw in keys:
print(kw, ":", keywords[kw])``````

``````cheeseshop("Limburger", "It's very runny, sir.",
"It's really very, VERY runny, sir.",
shopkeeper="Michael Palin",
client="John Cleese",
sketch="Cheese Shop Sketch")``````

``````-- Do you have any Limburger ?
-- I'm sorry, we're all out of Limburger
It's very runny, sir.
It's really very, VERY runny, sir.
----------------------------------------
client : John Cleese
shopkeeper : Michael Palin
sketch : Cheese Shop Sketch``````

### 任意参数列表

``````def write_multiple_items(file, separator, *args):
file.write(separator.join(args))``````

``````>>> def concat(*args, sep="/"):
...    return sep.join(args)
...
>>> concat("earth", "mars", "venus")
'earth/mars/venus'
>>> concat("earth", "mars", "venus", sep=".")
'earth.mars.venus'``````

### 参数列表解包

``````def cute(sex, age, height):
print(sex,age,height,end=' ')
print()

t = ['female', 18, 167]
cute(*t)

# output:
female 18 167 ``````

``````d = {'sex': 'male', 'age':18, 'height': 172}
cute(**d)

# output:
male 18 172 ``````

* 或 **操作符有两个作用：

*arg 表示分散的参数，接受分散的参数。解析后的 arg 表示tuple或者map，可以传入函数参数为元组的函数。

### lemos

Peach是一种用Python编写的 Fuzzer。这种工具有助于发现并公开许多漏洞，并认为是黑客和安全团体中最流行的工具之一。为了利用Peach框架，必须创建Phthon脚本，脚本 中包含了在服务器上执行的...

2013/02/06
8.8K
1

GroundworkCSS 是一个基于 Sass 预处理器的开源项目 ，主要用于快速构建响应式 Web 应用程序。拥有一个灵活的、可嵌套的网格系统，可以创建适应多种浏览设备的布局。 Groundwork 还提供多种 ...

2013/02/25
2.8W
4

DictForGeeks是个人写的一款词典软件，支持高度自定义。个人在学习过程中，发现很多科技词汇的意思跟主流的词义很不一样就想写一款可以自己定义词义的词典，支持添加和查询功能。至少要比txt...

2013/03/30
1K
0
Java™ 编译器--Janino

Janino是一个超级小但又超级快的Java™ 编译器. 它不仅能像javac工具那样讲一组源文件编译成字节码文件，还可以对一些Java表达式，代码块，类中的文本(class body)或者内存中源文件进行编译，...

2013/04/02
4.1K
0
Python开发者社区整站源码--Pythoner

pythoner.net 整站源代码 依赖模块 Django 1.4.2 PIL DjangoVerifyCode 0.2.2 开发环境配置 运行scripts目录下的setupenv.sh文件，将会自动安装配置所需环境 设置本地环境变量:export env=D...

~T.y~
2013/04/10
3.2K
0

1、简介 1.1、核心组件 SqlSessionFactoryBuilder(构造器)：根据配置信息或者代码生成SqlSessionFactory SqlSessionFactory(工厂接口)：依靠工厂来生成SqlSession(会话) SqlSession(会话): ...

5分钟前
0
0
Tomcat集群带来的问题和解决思路

Tomcat集群 存在问题 解决方案 基于Redis+Cookie+Jackson+Filter的原生解决集群Session共享问题 使用Spring Session零侵入解决Session共享 单点登录实现 Redis构建Session服务器 使用Redis+C...

code-ortaerc
25分钟前
17
0

V5codings
2019/11/19
0
0
ubuntu 安装 oh my zsh

https://github.com/ohmyzsh/ohmyzsh https://ohmyz.sh/#install 安装 sudo apt-get install zsh -ysh -c "\$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh ......

37分钟前
7
0

9
0