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. vue 项目的I18n国际化之路

    I18n (internationalization ) ---未完善 产品国际化是产品后期维护及推广中重要的一环,通过国际化操作使得产品能更好适应不同语言和地区的需求 国际化重点:1. 语言语言本地 ...

  2. PAT 1091. Acute Stroke (bfs)

    One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...

  3. 一个电商项目的Web服务化改造3:改进方案の规范和约定、单表、单一职责

         最近一直在做一个电商项目,需要把原有单系统架构的项目,改造成基于服务的架构,SOA.      有点挑战,做完了,会有很大进步. 上一篇,我们描述了原有项目中的问题.  或者说是,本篇的基本 ...

  4. java 同时安装多版本问题

    java 同时安装多版本问题(转) http://www.cnblogs.com/SamuelSun/p/6022296.html http://blog.csdn.net/u013256622/ar ...

  5. sql 语句实现可用户名、邮箱、手机号登录系统

    select top 1 nid from Users where (userName collate Chinese_PRC_CS_AS=@userName or mobile collate Ch ...

  6. [vagrant]第一次安装添加box出现问题汇总

    1.本地文件要加全文件名和协议file:/// 2.The box failed to unpackage properly. Please verify that the box file you' ...

  7. Eclipse ADT 导入别的电脑开发的项目

    用Eclipse开发的时候常常要导入别的电脑开发的项目,常常会出错,甚至导入不了. 方法一: 把你正在使用的Eclipse开发的随便一个项目.打开,把下图这三个文件复制过去你要导入的项目.覆盖.然后再 ...

  8. 【Hibernate学习】 ——ORM(四)再次认识实体继承

    在信用办时.做失信.守信.黑名单这一块的时候.先把原来的需求看了看.紧接着就開始设计实体,这一块大部分都是同样的信息,所以就设计了一个实体,而且用一个状态标识出来是失信.守信还是黑名单. 在之后的改动 ...

  9. 改动mysqlpassword

    1.假设没有password,则 mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); ...

  10. LinkedHashMap源代码阅读

    LinkedHashMap LinkedHashMap内部採用了散列表和链表实现Map接口,并能够保证迭代的顺序,和HashMap不同,其内部维护一个指向全部元素的双向链表,其决定了遍历的顺序,一般是 ...