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的更多相关文章

  1. Python Built-in Function 学习笔记

    Python Built-in Function 学习笔记 1. 匿名函数 1.1 什么是匿名函数 python允许使用lambda来创建一个匿名函数,匿名是因为他不需要以标准的方式来声明,比如def ...

  2. Python中function(函数)和methon(方法)的区别

    在Python中,对这两个东西有明确的规定: 函数function —— A series of statements which returns some value to a caller. It ...

  3. [Python] Python 之 function, unbound method 和 bound method

    首先看一下以下示例.(Python 2.7) #!/usr/bin/env python # -*- coding: utf-8 -*- class C(object): def foo(self): ...

  4. 分分钟钟学会Python - 函数(function)

    函数(function) 1 基本结构 本质:将多行代码拿到别处并起个名字,以后通过名字就可以找到这行代码并执行 应用场景: 代码重复执行 代码量很多超过一屏,可以选择通过函数进行代码的分割 写代码方 ...

  5. python build-in function

    目录(?)[-] absx alliterable anyiterable basestring binx boolx callableobject chri classmethodfunction ...

  6. python 函数function

    函数 当代码出现有规律的重复的时候,只写一次函数实现多次使用(调用) 可使用的函数: 自定义函数 内置函数:文档  https://docs.python.org/3/library/function ...

  7. Python partial function 偏函数

    Partial function 偏函数是将所要承载的函数作为partial()函数的第一个参数,原函数的各个参数依次作为partial()函数后续的参数,除非使用关键字参数. 当函数的参数个数太多, ...

  8. python define function

    >>> def square(x): ... 'calculates the square of the number x.' ... return x*x ... >> ...

  9. Python~recursive function递归函数

    尾递归实现循环 def fact(n): if n==1: return 1 else : return n * fact(n-1) raw_input() 字符而非数字 unsupported op ...

随机推荐

  1. Codeforces 919C - Seat Arrangements

    传送门:http://codeforces.com/contest/919/problem/C 给出一张n×m的座位表(有已占座位和空座位),请选择同一行(或列)内连续的k个座位.求选择的方法数. H ...

  2. BUPT2017 wintertraining(15) #9

    下面不再说明题意了请自行读题,直接放contest链接. https://vjudge.net/contest/151607 A.考虑当火车隔k站一停时 区间长度 >= k 的纪念品一定能买到 ...

  3. Codeforces Round #411(Div. 2)——ABCDEF

    30min水掉前面4T,30min尝试读懂EF题,60min划水 顺便D忘记取膜丢50分,距比赛结束10s时hack失败丢50分... 从2620掉分到2520,从rank227掉到rank354.. ...

  4. redis 初学

    1.网站:http://redis.cn/ 2.下载安装和配置 http://www.tuicool.com/articles/aQbQ3u 3.简述redis http://www.jb51.net ...

  5. nyoj_289_苹果_20140307

    苹果 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 ctest有n个苹果,要将它放入容量为v的背包.给出第i个苹果的大小和价钱,求出能放入背包的苹果的总价钱最大值. ...

  6. BZOJ——T 1800: [Ahoi2009]fly 飞行棋

    Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1767  Solved: 1395[Submit][Status][Discuss] Descripti ...

  7. Spring MVC-处理程序映射(Handler Mapping)-Bean名称Url处理程序映射(Bean Name Url Handler Mapping)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_beannameurlhandlermapping.htm 说明:示例基于Spri ...

  8. servlet和Spring的DispatcherServlet详解

    Servlet是什么 1. Servlet是服务器端运行的一个程序,是一个被编译好的Java类.它不是框架等. 2. Web容器的启动,需要依赖Servlet.当web服务器开始执行时,servlet ...

  9. php session自定义处理

    原文:http://www.cnblogs.com/mrcoke/  这个人的博客上转的. 这个博客也好: 学算法和数据结构!!http://blog.csdn.net/21aspnet/articl ...

  10. Linux 大规模请求server连接数相关设置

    一般一个大规模Linuxserver请求数可能是几十万上百万的情况.须要足够的连接数来使用,所以务必进行对应的设置. 默认的Linuxserver文件描写叙述符等打开最大是1024.用ulimit - ...