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. Python编程时.py与.pyc文件的介绍

    Python的程序中,是把原始程序代码放在.py文件里,而Python会在执行.py文件的时候.将.py形式的程序编译成中间式文件(byte-compiled)的.pyc文件,这么做的目的就是为了加快 ...

  2. C#学习笔记_14_接口&命名空间

    14_接口&命名空间 接口 一系列规范 语法: 如果一个类的后面既有父类也有接口,那么一定是父类在前,接口靠后 一个类可以实现多个接口 规范:接口命名以大写字母 I 开头 接口中可以包含接口方 ...

  3. 学习Spring框架等技术的方向、方法和动机

    学习Spring框架最早学习Spring框架是在大二的时候,当时看了几本书,看了一些视频,主要是传智播客的.更多的,还是写代码,单独写Spring的,也有与Struts和Hibernate等框架整合的 ...

  4. SVN学习总结(3)——分支合并

    首先在主干trunck上,创建分支branch1.0.0,如下图: 从主干切换到branch1.0.0,在branch1.0.0中添加test.java文件, 分支与主干的合并,首先应将主干trunc ...

  5. 火星人 2004年NOIP全国联赛普及组

    题目描述 Description 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法.这种交流方法是这样的,首先,火星人把一个 ...

  6. 20180530利用Maxwell组件实时监听Mysql的binlog日志

    转自:https://blog.csdn.net/qq_30921461/article/details/78320750 http://kafka.apache.org/quickstart htt ...

  7. K - Count the string kmp_Next数组应用

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  8. Oracle-时间与字符串转换

    --UPDATE_TIME 是时间,直接转成字符串 SELECT to_char(MAX(UPDATE_TIME), 'yyyy-mm-dd hh24:mi:ss') as lastUpdateTim ...

  9. Linux下安装lvs

    lvs已经编译到linux内核中,仅仅须要安装lvs的管理软件ipvsadm就可以 1. 插入光盘.查找设备 [root@chen ~]# ls -l /dev | grep cdrom lrwxrw ...

  10. [Javascript] AbortController to cancel the fetch request

    We are able to cancel the fetch request by using AbortController with RxJS Observable. return Observ ...