Python中的高阶消息 [英] High Order Messages in Python

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

问题描述

我正在读高阶信息。在Ruby中由Nat Pryce和

思考它是否可以是util,如果可以的话,如果它可以在Python中完成。

有人已经尝试了吗?


参考文献:
http:/ /lambda-the-ultimate.org/node/view/1047
http://nat.truemesh.com/archives/000535.html
http://nat.truemesh.com/archives/000537.html

I''m reading about "high order messages" in Ruby by Nat Pryce, and
thinking if it could be util and if so, if it could be done in Python.
Someone already tried?

References:
http://lambda-the-ultimate.org/node/view/1047
http://nat.truemesh.com/archives/000535.html
http://nat.truemesh.com/archives/000537.html

推荐答案

哼哼......我觉得你没有得到这样的意思:我不是在说高级订单

功能。

什么叫做高阶方法有些人喜欢连接一些

''通用''方法,用于做这样的事情:

claimants.where.retired?.do.receive_benefit 50

在第一篇文章中第2和第3个链接与

最相关的概念。阅读本文:
http://www.metaobject.com/论文/ Hig ... OPSLA_2005.pdf

Hum... I thnk you dont get the ideia: I''m not talking abou High Order
Functions.
What ho call "High Order Methods is some like "connecting" some
''generic'' methods created to do things like this:
claimants.where.retired?.do.receive_benefit 50
The 2nd and 3rd links that in the first post is the most relevant to
undestand the concept. Read this too:
http://www.metaobject.com/papers/Hig...OPSLA_2005.pdf


这可以适用于使用更高阶的Python。

函数。它不是Ruby版本,因为Python

允许你使用函数作为一流的对象,使得

All-You-Can-Do-Is-变得复杂Pass-A-Message哲学。这是我的5分钟

实现:

class HigherOrderList(list):


def do(self,func):

返回HigherOrderList(每个(self,func))


def where(self,pred):

返回HigherOrderList(mass_test (self,pred))


def mass_test(iterable,pred):

for iterable中的项目:

if pred( item):

收益项目

def每个(可迭代,方法):

for iterable中的项目:

收益率方法(项目)

This can be suitably applied to Python with the use of Higher Order
Functions, though. It''s not quite the Ruby version because Python
allows you to use functions as first-class objects, complicating the
All-You-Can-Do-Is-Pass-A-Message philosophy. This is my 5-minute
implementation:
class HigherOrderList(list):

def do(self, func):
return HigherOrderList(each(self, func))

def where(self, pred):
return HigherOrderList(mass_test(self, pred))

def mass_test(iterable, pred):
for item in iterable:
if pred(item):
yield item

def each(iterable, method):
for item in iterable:
yield method(item)


" ed ************ @ gmail.com" < ED ************ @ gmail.com>写道:
"ed************@gmail.com" <ed************@gmail.com> writes:
我正在阅读高阶信息。在Ruby中由Nat Pryce编写,并且思考它是否可以是util,如果可以的话,是否可以在Python中完成。
有人已经尝试过了吗?
I''m reading about "high order messages" in Ruby by Nat Pryce, and
thinking if it could be util and if so, if it could be done in Python.
Someone already tried?




是的,我很确定它可以在Python中完成。所有它真正需要

是能够捕获对未定义属性的引用,这是
Python所具有的。你使你的集合类的HOM返回一个对象

并引用self,并且该类的__getattr__方法

然后在引用的集合中的每个对象上调用getattr

未定义方法的实例,操作结果为适合该HOM的

,并返回一个新列表。


但是这真的不适合Python。对于更积极的OO语言,这实际上是一个

功能。为了使示例HOMs

描述真的很有用,你想将它们添加到

集合的某个基类中。但是这个基类在Python中是不存在的 - Python只是

不是那个OO。


你可以在列表,元组中添加各种HOM ,生成器和迭代器

- 但是任何想要创建新序列类的人都会有

来添加客户可能想要使用的所有HOM。

pythonic方式是添加一个与所有

各种序列类型一起使用的函数 - 然后它将自动与任何

用户一起使用 - 像序列一样嘎嘎叫的定义类。它不是很好OO -

但它是pythonic。实际上,Python已经具有在你发布的链接中捕获示例HOM功能的函数:

和除非由过滤器处理。 in_order_of和in_reverse_order_of

由sorted排序。 do是由地图处理。当然,最近的

版本的python提供的列表理解比这些函数的某些更好。


那就是说,HOM这是一个*非常强大的机制。示例 - 和

我对它们的讨论 - 仅涵盖一个非常广泛的用例。有可能

是其他更适合Python的人。举例来说这些东西可能是值得的。


< mike

-

Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。



Yes, I''m pretty sure it could be done in Python. All it really needs
is the ability to catch references to undefined attributes, which
Python has. You make the HOM of your collection class return an object
with a reference to self, and the __getattr__ method of that classs
then invokes getattr on each object in the referenced collection
instance for the undefined method, manipulating the result as
appropriate for that HOM, and returning a new list.

But this really isn''t a very good fit for Python. This is really a
feature for more aggressively OO languages. To make the examples HOMs
described really useful, you want to add them to some base class for
collections. But that base class doesn''t exist in Python - Python just
isn''t that OO.

You could add various HOMs to lists, tuples, generators and iterators
- but then anyone who wanted to create a new sequence class would have
to add all of the HOMs that their clients might want to use. The
pythonic way would be to add a function that works with all the
various sequence types - which would then automatically work with any
user-defined classes that quacked like a sequence. It''s not very OO -
but it is pythonic. In fact, Python already has functions that capture
the functionality of the example HOMs in the links you posted: where
and unless are handled by filter. in_order_of and in_reverse_order_of
are handled by sorted. do is is handled by map. Of course, recent
versions of python provide list comprehensions as preferable to some
of these functions.

That said, HOM''s are a *very* powerful mechanism. The examples - and
my discussion of them - cover just one very broad use case. There may
be others where they are a better fit with Python. Having examples of
how to do these kinds of things around is probably worthwhile.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


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

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