1.python函数的闭包怎么理解
1. 闭包的概念首先还得从基本概念说起,什么是闭包呢?来看下维基上的解释:复制代码代码如下:在计算机科学中,闭包(Closure)是词法闭包(Lexical Closure)的简称,是引用了自由变量的函数。
这个被引用的自由变量将和这个函数一同存在,即使已经离开了创造它的环境也不例外。所以,有另一种说法认为闭包是由函数和与其相关的引用环境组合而成的实体。
闭包在运行时可以有多个实例,不同的引用环境和相同的函数组合可以产生不同的实例。
.上面提到了两个关键的地方: 自由变量 和 函数, 这两个关键稍后再说。还是得在赘述下“闭包”的意思,望文知意,可以形象的把它理解为一个封闭的包裹,这个包裹就是一个函数,当然还有函数内部对应的逻辑,包裹里面的东西就是自由变量,自由变量可以在随着包裹到处游荡。
当然还得有个前提,这个包裹是被创建出来的。在通过Python的语言介绍一下,一个闭包就是你调用了一个函数A,这个函数A返回了一个函数B给你。
这个返回的函数B就叫做闭包。你在调用函数A的时候传递的参数就是自由变量。
举个例子:复制代码代码如下:def func(name):def inner_func(age):print 'name:', name, 'age:', agereturn inner_funcbb = func('the5fire')bb(26) # >>> name: the5fire age: 26这里面调用func的时候就产生了一个闭包——inner_func,并且该闭包持有自由变量——name,因此这也意味着,当函数func的生命周期结束之后,name这个变量依然存在,因为它被闭包引用了,所以不会被回收。另外再说一点,闭包并不是Python中特有的概念,所有把函数做为一等公民的语言均有闭包的概念。
不过像Java这样以class为一等公民的语言中也可以使用闭包,只是它得用类或接口来实现。更多概念上的东西可以参考最后的参考链接。
2. 为什么使用闭包基于上面的介绍,不知道读者有没有感觉这个东西和类有点相似,相似点在于他们都提供了对数据的封装。不同的是闭包本身就是个方法。
和类一样,我们在编程时经常会把通用的东西抽象成类,(当然,还有对现实世界——业务的建模),以复用通用的功能。闭包也是一样,当我们需要函数粒度的抽象时,闭包就是一个很好的选择。
在这点上闭包可以被理解为一个只读的对象,你可以给他传递一个属性,但它只能提供给你一个执行的接口。因此在程序中我们经常需要这样的一个函数对象——闭包,来帮我们完成一个通用的功能,比如后面会提到的——装饰器。
3. 使用闭包第一种场景 ,在python中很重要也很常见的一个使用场景就是装饰器,Python为装饰器提供了一个很友好的“语法糖”——@,让我们可以很方便的使用装饰器,装饰的原理不做过多阐述,简言之你在一个函数func上加上@decorator_func, 就相当于decorator_func(func):复制代码代码如下:def decorator_func(func):def wrapper(*args, **kwargs):return func(*args, **kwargs)return wrapper@decorator_funcdef func(name):print 'my name is', name# 等价于decorator_func(func)在装饰器的这个例子中,闭包(wrapper)持有了外部的func这个参数,并且能够接受外部传过来的参数,接受过来的参数在原封不动的传给func,并返回执行结果。这是个简单的例子,稍微复杂点可以有多个闭包,比如经常使用的那个LRUCache的装饰器,装饰器上可以接受参数@lru_cache(expire=500)这样。
实现起来就是两个闭包的嵌套:复制代码代码如下:def lru_cache(expire=5):# 默认5s超时def func_wrapper(func):def inner(*args, **kwargs):# cache 处理 bala bala balareturn func(*args, **kwargs)return innerreturn func_wrapper@lru_cache(expire=10*60)def get(request, pk)# 省略具体代码return response()不太懂闭包的同学一定得能够理解上述代码,这是我们之前面试经常会问到的面试题。第二个场景 ,就是基于闭包的一个特性——“惰性求值”。
这个应用比较常见的是在数据库访问的时候,比如说:复制代码代码如下:# 伪代码示意class QuerySet(object):def __init__(self, sql):self.sql = sqlself.db = Mysql.connect().corsor() # 伪代码def __call__(self):return db.execute(self.sql)def query(sql):return QuerySet(sql)result = query("select name from user_app")if time > now:print result # 这时才执行数据库访问上面这个不太恰当的例子展示了通过闭包完成惰性求值的功能,但是上面query返回的结果并不是函数,而是具有函数功能的类。有兴趣的可以去看看Django的queryset的实现,原理类似。
第三种场景 , 需要对某个函数的参数提前赋值的情况,当然在Python中已经有了很好的解决访问 functools.parial,但是用闭包也能实现。复制代码代码如下:def partial(**outer_kwargs):def wrapper(func):def inner(*args, **kwargs):for k, v in outer_kwargs.items():kwargs[k] = vreturn func(*args, **kwargs)return innerreturn wrapper@partial(age=15)def say(name=None, age=None):print name, agesay(name="the5fire")# 当然用functools比这个简单多了# 只需要: f。
2.python function
恰好做过。
您安好.----------------------------# File : warmup.pydef lines_starts_with(f, s):____"""Return a list containing the lines of the given open file that start with____the given str. Exclude leading and trailing whitespace.________Arguments:____- `f`: the file____- `s`: str at the beginning of the line____"""____result = []____for line in f:________if line.startswith(s):____________result.append(line.strip())____return resultdef file_to_dictionary(f, s):____"""Given an open file that contains a unique string followed by a given____string delimiter and more text on each line, return a dictionary that____contains an entry for each line in the file: the unique string as the key____and the rest of the text as the value (excluding the delimiter, and leading____and trailing whitespace).________Arguments:____- `f`: the file____- `s`: the delimiter str____"""____result = {}____for line in f:________k, _, v = line.partition(s)________result[k.strip()] = v.strip()____return resultdef merge_dictionaries(d1, d2):____"""Return a dictionary that is the result of merging the two given____dictionaries. In the new dictionary, the values should be lists. If a key is____in both of the given dictionaries, the value in the new dictionary should____contain both of the values from the given dictionaries, even if they are the____same.____merge_dictionaries({ 1 : 'a', 2 : 9, -8 : 'w'}, {2 : 7, 'x' : 3, 1 : 'a'})____should return {1 : ['a', 'a'], 2 : [9, 7], -8 : ['w'], 'x' : [3]} ____Arguments:____- `d1`, 'd2': dicts to be merged____"""____result = {}____for key in d1:________if key in d2:____________result[key] = [d1[key], d2[key]]________else:____________result[key] = [d1[key]]____for key in d2:________if not key in result:____________result[key] = [d2[key]]____return result-----------------------------------------------------# File fasta.pyfrom warmup import *def count_sequences(f):____"""Return the number of FASTA sequences in the given open file. Each____sequence begins with a single line that starts with >.________Arguments:____- `f`: the file____"""____return len(lines_starts_with(f, '>'))def get_sequence_list(f):____"""Return a nested list where each element is a 2-element list containing a____FASTA header and a FASTA sequence from the given open file (both strs).________Arguments:____- `f`: the file____"""____result = []____current_key = ''____current_data = ''____for line in f:________if line.startswith('>'):____________if current_data:________________result.append([current_key, current_data])________________current_data = ''____________current_key = line.strip()________else:____________current_data+= line.strip()____result.append([current_key, current_data])____return result____def merge_files(f1, f2):____"""Return a nested list containing every unique FASTA header and sequence____pair from the two open input files. Each element of the list to be returned____is a 2-element list containing a FASTA header and a FASTA sequence.________Arguments:____- `f1`, 'f2': The File ____"""____seq = get_sequence_list(f1) + get_sequence_list(f2)____seq = list( set(seq) )____return seqdef get_codons(s):____"""Return a list of strings containing the codons in the given DNA sequence.________Arguments:____- `s`: DNA sequence, divied by 3____"""____return [s[3*i : 3*(i+1)] for i in xrange(len(s)/3)]def get_amino_name_dict(f):____"""Return a dictionary constructed from the given open file, where the keys____are amino acid codes and the values are the amino acid names.________Arguments:____- `f`: the file, like amino_names.txt____"""____return file_to_dictionary(f, ' ')def get_codon_amino_dict(f):____"""Return a dictionary where the keys are codons and the values are amino____acid codes from the given open file.________Arguments:____- `f`: the file, like codon_aminos.txt ____"""____return file_to_dictionary(f, ':')def translate(s, d):____"""Return the given DNA sequence (the str parameter) translated into its____amino acid codes, according to the given dictionary where the keys are____codons and the values are amino acid codes. You may assume that the length____of the string is divisible by 3.________Arguments:____- `s`: given DNA sequence____- `d`: given dictionary____"""____codons = get_codons(s)____result = []____for i in codes:________result.append(d[i])____return resultdef codon_to_name(s, d1, d2):____"""Return the name of the amino acid for the given codon (the str____parameter).________Arguments:____- `s`: given codon ____- `d1`: codons for keys and amino acid codes。
3.Python里method和function的区别
呃,确实误导了。
function 和 method 是两个不同的语境下的概念。function 是从代码机制角度来说的,表示函数这个机制,它是与“语句”相对应的概念。而 method 是从面向对象机制角度来说的,它表示类或对象的一个方法(也称操作),它是与“属性”等相对应的概念。
通常来说,function 与 method 是可以互换的。严格地说,虽然方法是由函数来实现的,但所有的函数并不一定是方法,例如:构造函数、析构函数、属性访问器等等都是由函数实现的,但它们都不是方法。有些私有函数的设计意图也是从面向过程来考虑的,并不是要提供一个 method。
4.python函数的闭包怎么理解
1. 闭包的概念 首先还得从基本概念说起,什么是闭包呢?来看下维基上的解释:复制代码代码如下:在计算机科学中,闭包(Closure)是词法闭包(Lexical Closure)的简称,是引用了自由变量的函数。
这个被引用的自由变量将和这个函数一同存在,即使已经离开了创造它的环境也不例外。所以,有另一种说法认为闭包是由函数和与其相关的引用环境组合而成的实体。
闭包在运行时可以有多个实例,不同的引用环境和相同的函数组合可以产生不同的实例。
.上面提到了两个关键的地方: 自由变量 和 函数, 这两个关键稍后再说。还是得在赘述下“闭包”的意思,望文知意,可以形象的把它理解为一个封闭的包裹,这个包裹就是一个函数,当然还有函数内部对应的逻辑,包裹里面的东西就是自由变量,自由变量可以在随着包裹到处游荡。
当然还得有个前提,这个包裹是被创建出来的。在通过Python的语言介绍一下,一个闭包就是你调用了一个函数A,这个函数A返回了一个函数B给你。
这个返回的函数B就叫做闭包。你在调用函数A的时候传递的参数就是自由变量。
举个例子:复制代码代码如下:def func(name):def inner_func(age):print 'name:', name, 'age:', age return inner_func bb = func('the5fire') bb(26) # >>> name: the5fire age: 26 这里面调用func的时候就产生了一个闭包——inner_func,并且该闭包持有自由变量——name,因此这也意味着,当函数func的生命周期结束之后,name这个变量依然存在,因为它被闭包引用了,所以不会被回收。另外再说一点,闭包并不是Python中特有的概念,所有把函数做为一等公民的语言均有闭包的概念。
不过像Java这样以class为一等公民的语言中也可以使用闭包,只是它得用类或接口来实现。更多概念上的东西可以参考最后的参考链接。
2. 为什么使用闭包 基于上面的介绍,不知道读者有没有感觉这个东西和类有点相似,相似点在于他们都提供了对数据的封装。不同的是闭包本身就是个方法。
和类一样,我们在编程时经常会把通用的东西抽象成类,(当然,还有对现实世界——业务的建模),以复用通用的功能。闭包也是一样,当我们需要函数粒度的抽象时,闭包就是一个很好的选择。
在这点上闭包可以被理解为一个只读的对象,你可以给他传递一个属性,但它只能提供给你一个执行的接口。因此在程序中我们经常需要这样的一个函数对象——闭包,来帮我们完成一个通用的功能,比如后面会提到的——装饰器。
3. 使用闭包 第一种场景 ,在python中很重要也很常见的一个使用场景就是装饰器,Python为装饰器提供了一个很友好的“语法糖”——@,让我们可以很方便的使用装饰器,装饰的原理不做过多阐述,简言之你在一个函数func上加上@decorator_func, 就相当于decorator_func(func):复制代码代码如下:def decorator_func(func):def wrapper(*args, **kwargs):return func(*args, **kwargs) return wrapper @decorator_func def func(name):print 'my name is', name# 等价于 decorator_func(func) 在装饰器的这个例子中,闭包(wrapper)持有了外部的func这个参数,并且能够接受外部传过来的参数,接受过来的参数在原封不动的传给func,并返回执行结果。这是个简单的例子,稍微复杂点可以有多个闭包,比如经常使用的那个LRUCache的装饰器,装饰器上可以接受参数@lru_cache(expire=500)这样。
实现起来就是两个闭包的嵌套:复制代码代码如下:def lru_cache(expire=5):# 默认5s超时 def func_wrapper(func):def inner(*args, **kwargs):# cache 处理 bala bala bala return func(*args, **kwargs) return inner return func_wrapper @lru_cache(expire=10*60) def get(request, pk)# 省略具体代码 return response() 不太懂闭包的同学一定得能够理解上述代码,这是我们之前面试经常会问到的面试题。第二个场景 ,就是基于闭包的一个特性——“惰性求值”。
这个应用比较常见的是在数据库访问的时候,比如说:复制代码代码如下:# 伪代码示意 class QuerySet(object):def __init__(self, sql):self.sql = sql self.db = Mysql.connect().corsor() # 伪代码 def __call__(self):return db.execute(self.sql) def query(sql):return QuerySet(sql) result = query("select name from user_app") if time > now:print result # 这时才执行数据库访问 上面这个不太恰当的例子展示了通过闭包完成惰性求值的功能,但是上面query返回的结果并不是函数,而是具有函数功能的类。有兴趣的可以去看看Django的queryset的实现,原理类似。
第三种场景 , 需要对某个函数的参数提前赋值的情况,当然在Python中已经有了很好的解决访问 functools.parial,但是用闭包也能实现。复制代码代码如下:def partial(**outer_kwargs):def wrapper(func):def inner(*args, **kwargs):for k, v in outer_kwargs.items():kwargs[k] = v return func(*args, **kwargs) return inner return wrapper @partial(age=15) def say(name=None, age=None):print name, age say(name="the5fire")# 当然用functools比这个简单多了# 只需要: functools.partial(say, age=15)(name='the5fire') 看起来这又是一个牵强的例子,不过也。
5.Python里method和function的区别
呃,确实误导了。
function 和 method 是两个不同的语境下的概念。function 是从代码机制角度来说的,表示函数这个机制,它是与“语句”相对应的概念。
而 method 是从面向对象机制角度来说的,它表示类或对象的一个方法(也称操作),它是与“属性”等相对应的概念。通常来说,function 与 method 是可以互换的。
严格地说,虽然方法是由函数来实现的,但所有的函数并不一定是方法,例如:构造函数、析构函数、属性访问器等等都是由函数实现的,但它们都不是方法。有些私有函数的设计意图也是从面向过程来考虑的,并不是要提供一个 method。
转载请注明出处代码入门网 » pythonfunction