Python之function
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
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之function的更多相关文章
- Python Built-in Function 学习笔记
Python Built-in Function 学习笔记 1. 匿名函数 1.1 什么是匿名函数 python允许使用lambda来创建一个匿名函数,匿名是因为他不需要以标准的方式来声明,比如def ...
- Python中function(函数)和methon(方法)的区别
在Python中,对这两个东西有明确的规定: 函数function —— A series of statements which returns some value to a caller. It ...
- [Python] Python 之 function, unbound method 和 bound method
首先看一下以下示例.(Python 2.7) #!/usr/bin/env python # -*- coding: utf-8 -*- class C(object): def foo(self): ...
- 分分钟钟学会Python - 函数(function)
函数(function) 1 基本结构 本质:将多行代码拿到别处并起个名字,以后通过名字就可以找到这行代码并执行 应用场景: 代码重复执行 代码量很多超过一屏,可以选择通过函数进行代码的分割 写代码方 ...
- python build-in function
目录(?)[-] absx alliterable anyiterable basestring binx boolx callableobject chri classmethodfunction ...
- python 函数function
函数 当代码出现有规律的重复的时候,只写一次函数实现多次使用(调用) 可使用的函数: 自定义函数 内置函数:文档 https://docs.python.org/3/library/function ...
- Python partial function 偏函数
Partial function 偏函数是将所要承载的函数作为partial()函数的第一个参数,原函数的各个参数依次作为partial()函数后续的参数,除非使用关键字参数. 当函数的参数个数太多, ...
- python define function
>>> def square(x): ... 'calculates the square of the number x.' ... return x*x ... >> ...
- Python~recursive function递归函数
尾递归实现循环 def fact(n): if n==1: return 1 else : return n * fact(n-1) raw_input() 字符而非数字 unsupported op ...
随机推荐
- mysql登录出现1045错误
这个问题是在window server 2012上安装mysql之后, 远程访问时出现的1045错误 我新建了一个相同的用户用于远程访问, 密码也相同, 但是还是访问不了 参照链接:https://b ...
- python--(十步代码学会线程)
python--(十步代码学会线程) 一.线程的创建 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getname(): 返回线程名. # setName(): 设置 ...
- Flask - 模板语言jinja2 和render_template高级用法
目录 Flask - 模板语言jinja2 和render_template高级用法 一. 字典传递至前端 二. 列表传入前端Jinja2 模板的操作: 三. 大字典传入前端 Jinja2 模板 四. ...
- seminar information (Email template)
The following is an email example of seminar information **************** Dear all, It is a plea ...
- 腾讯云,搭建Docker环境
安装与配置 Docker 任务时间:10min ~ 20min 安装 Docker Docker 软件包已经包括在默认的 CentOS-Extras 软件源里.因此想要安装 docker,只需要运行下 ...
- PAT 1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- Python网络请求urllib和urllib3详解
Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urlli ...
- IDEA 工具使用报错总结
读前语:此文章仅给非入门级观看 1.使用Debug 无法运行,而使用Run 则正常启动,报错代码如下 1 Error running 'jx_web': Unable to open debugge ...
- 如何判断windows动态链接库是32还是64位
如果安装过Visual Studio的话,直接打开一个VS提供的控制台窗口,比如VS2012 x64 Native Tools Command Prompt. 用下面的命令查看程序的头部信息:“dum ...
- android NDK开发在本地C/C++源码中设置断点单步调试具体教程
近期在学android NDK开发,折腾了一天,最终可以成功在ADT中设置断点单步调试本地C/C++源码了.网上关于这方面的资料太少了,并且大都不全,并且调试过程中会出现各种各样的问题,真是非常磨人. ...