Python中的闭包/块 [英] Closures / Blocks in Python

查看:75
本文介绍了Python中的闭包/块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道在Python中使用闭包或块的方法,比如Ruby中使用的
?特别是在{}括号中使用的那些。

Does anyone know a way to use closures or blocks in python like those
used in Ruby? Particularly those used in the { } braces.

推荐答案

在星期二,2007-07-24在14:58 +0000,treble54写道:
On Tue, 2007-07-24 at 14:58 +0000, treble54 wrote:

有没有人知道在Python中使用闭包或块的方法,比如那些在Ruby中使用的
?特别是在{}括号中使用的那些。
Does anyone know a way to use closures or blocks in python like those
used in Ruby? Particularly those used in the { } braces.



请描述您要解决的问题。即使Python有一个

直接相当于Ruby闭包或块,我也不认为它是b $ b,它可能不是最好的解决方案你的问题。


-

Carsten Haese
http://informixdb.sourceforge.net


2007-07-24,treble54< tr **** **@gmail.com写道:
On 2007-07-24, treble54 <tr******@gmail.comwrote:

有没有人知道在Python中使用闭包或块的方法,比如

Ruby中使用的那些?特别是在{}括号中使用的那些。
Does anyone know a way to use closures or blocks in python like
those used in Ruby? Particularly those used in the { } braces.



Python的无名函数是一周。所以它支持迭代器

和生成器使用协议,理解和一些简单的

语句,而不是推广使用无名函数。


Ruby's


some_list.each do | item |

put item

end


如果我理解正确,在Python中将是:


for some_list中的项目:

打印项目


适用于任何支持迭代器协议的对象。


-

Neil Cerutti

Python''s nameless functions are week. So it supports iterators
and generators using protocols, comprehensions and a few simple
statements, rather than promoting the use of nameless functions.

Ruby''s

some_list.each do |item|
puts item
end

if I understood it correctly, In Python would be:

for item in some_list:
print item

That works for any object that supports the iterator protocol.

--
Neil Cerutti


7月24日上午8:58,treble54< trebl ... @ gmail.comwrote:
On Jul 24, 8:58 am, treble54 <trebl...@gmail.comwrote:

有没有人知道如何通过在Python中使用闭包或块,比如Ruby中使用的
?特别是在{}括号中使用的那些。
Does anyone know a way to use closures or blocks in python like those
used in Ruby? Particularly those used in the { } braces.



Python不是Ruby。 Python有一个lambda函数用于创建

匿名函数,但许多常见用例已经过期了,因为它们引入了迭代器和理解。 Python的函数是
第一类对象,可以传递,绑定到名称,并像其他对象一样使用
。 (我不知道Ruby的函数是否是
是第一类对象。)Python的函数对象是可调用的,但是

所以是类(调用他们创建了一个类实例)和一些

实例(那些定义__call__特殊方法的实例)。


如果你找不到办法你想用迭代器,

comprehensions或lambda,考虑编写一个小函数。哎呀,

你甚至可以在Python中嵌套函数或者传递一个函数作为

参数。


例如,删除所有从

名称列表中以''J'开头的名称:

newListOfNames = [nameList中名称的名称,如果不是

name.startswith(''J'')] #List comprehension

newListOfNames = filter(lambda name:not name.startswith(''J''),

nameList )#Silter with lambda


#明确的for-loop

newListOfNames = []

nameList中的名字:

如果不是name.startswith(''J''):newListOfNames.append(name)

看看http://ivan.truemesh.com/archives/ 000392.html"对于一些简单的Ruby代码和Python之间的比较。希望这会有所帮助。


--Jason

Python isn''t Ruby. Python has a lambda function for creating
anonymous functions, but many of the common use cases expired with the
introduction of iterators and comprehensions. Python''s functions are
first class objects, and can be passed around, bound to names, and
used like any other object. (I don''t know whether Ruby''s functions
are first class objects.) Python''s function objects are callable, but
so are classes (calling them creates a class instance) and some
instances (those that define the __call__ special method).

If you can''t find a way of doing what you want with iterators,
comprehensions, or lambda, consider writing a little function. Heck,
you can even nest functions in Python or pass a function as a
parameter.

For example, removing all names that start with a ''J'' from a list of
names:
newListOfNames = [ name for name in nameList if not
name.startswith(''J'') ] # List comprehension
newListOfNames = filter(lambda name: not name.startswith(''J''),
nameList) # Filter with lambda

# Explicit for-loop
newListOfNames = []
for name in nameList:
if not name.startswith(''J''): newListOfNames.append(name)
Take a look at "http://ivan.truemesh.com/archives/000392.html" for a
comparison between some simple Ruby code and Python. Hope this helps.

--Jason


这篇关于Python中的闭包/块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆