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. linux - redis基础

    目录 linux - redis基础 redis 源码编译安装 redis 数据结构 1. strings类型 2. list 类型 3. sets集合类型 有序集合 5. 哈希数据结构 centos ...

  2. Dijkstra算法求最短路径

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h&g ...

  3. GeoTrust 企业(OV)型 通配符(Wildcard) SSL证书

      GeoTrust 企业(OV)型 通配符(Wildcard)SSL证书(GeoTrust True BusinessID Wildcard SSL Certificates),支持通配符(Wild ...

  4. hdu 3572 最大流判断满流

    #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define ...

  5. nyoj_102_次方求模_201308221547

    次方求模时间限制:1000 ms  |  内存限制:65535 KB 难度:3描述 求a的b次方对c取余的值 输入 第一行输入一个整数n表示测试数据的组数(n<100)每组测试只有一行,其中有三 ...

  6. PHP array_fill()

    定义和用法 array_fill() 函数用给定的值填充数组,返回的数组有 number 个元素,值为 value.返回的数组使用数字索引,从 start 位置开始并递增.如果 number 为 0 ...

  7. 微信推送给服务器的XML消息解析-springmvc 解析xml数据流

    微信推送给服务器的XML消息解析: 可以使用request.getInputStream(); 获取输入的消息流:但是需要自己解析流: spring mvc自带解析功能: controller中: @ ...

  8. 【2014秋季版】【辛星php】【0】清晰的认识一下PHP语言

    *********************PHP情结***************** 1.假设您和我经历非常相似,也可能会有这种PHP情结,为什么呢.由于我最先学习的是Java.然后学习了C++,开 ...

  9. pascal+sublime搭建Pascal学习环境

    一.fpc安装 1. 下载:http://www.freepascal.org/down/i386/win32.var(或者:http://download.csdn.net/detail/wenph ...

  10. iOS刷新某个cell时候crash

    //一个section刷新     NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];     [tableview reloadSec ...