Python之function
2015-06-10 23:01
781 查看
1 Function
a function is a device that groups a set of statements so they can be run more than once in a program.
1) def statements
Function bodies often contain a return statement:
2) def executes at runtime
One way to understand this code is to realize that the def is much like an = statement: it simply assigns a name at runtime.
Unlike C, Python functions do not need to be fully defined before the program runs. More generally, defs are not evaluated until they are reached and run, and the code inside defs is not evaluated until the functions are later called.
Because function definition happens at runtime, there’s nothing special about function name. What’s important is the object to which it refers:
2 Example 1 - Definitions and Calls
1) definition
2) call
3 Example 2 - Intersecting Sequences
1) definition
2) call
3) polymorphism revisited
a function is a device that groups a set of statements so they can be run more than once in a program.
1) def statements
def <name>(arg1, arg2,... argN): <statements>
Function bodies often contain a return statement:
def <name>(arg1, arg2,... argN): ... return <value>
2) def executes at runtime
if test: def func(): # Define func this way ... else: def func(): # Or else this way ... ... func() # Call the version selected and buil
One way to understand this code is to realize that the def is much like an = statement: it simply assigns a name at runtime.
Unlike C, Python functions do not need to be fully defined before the program runs. More generally, defs are not evaluated until they are reached and run, and the code inside defs is not evaluated until the functions are later called.
Because function definition happens at runtime, there’s nothing special about function name. What’s important is the object to which it refers:
othername = func # Assign function object othername() # Call func again
2 Example 1 - Definitions and Calls
1) definition
>>> def times(x,y): ... return x * y ...
2) call
>>> times(4,5) # Arguments in parentheses 20
>>> x = times(3.14,4) >>> x # Save the result object 12.56
>>> times('Ha',4) # Functions are "typeless" 'HaHaHaHa'
3 Example 2 - Intersecting Sequences
1) definition
>>> def intersect(seq1,seq2): ... res = [] # Start empty ... for x in seq1: # Scan seq1 ... if x in seq2: # Common item? ... res.append(x) # Add to end ... return res ...
2) call
>>> s1 = "SPAM" >>> s2 = "SCAM" >>> intersect(s1,s2) # Strings ['S', 'A', 'M']
>>> [x for x in s1 if x in s2] ['S', 'A', 'M']
3) polymorphism revisited
>>> x = intersect([1,2,3],(1,4)) # Mixed types >>> x # Saved result object [1]
相关文章推荐
- Python HAIL CAESAR (凯撒密码) 1
- Python基础:27执行环境
- 机器学习三 -- 用Python实现K-近邻算法
- 通过SecureCRT对python操作时无法正确使用 方向键 退格键
- python第三方模块之pyquery
- Python图像处理库Pillow入门
- Python基础:26模块
- python 多线程
- Python 随机数抽取
- python whl 格式安装
- python 安装 ghosts
- [Python] 目录和文件操作
- Python 显示LinkedIn用户作业
- Python面试题集答案(2)
- Python之global
- 学习笔记:python
- python爬虫解决百度贴吧登陆验证码问题
- Python 的stat 模块
- 用Python写了一个postgresql函数,感觉很爽
- 用Python写了一个postgresql函数,感觉很爽