如何枚举类方法然后将它们与 python 中的 itertools.product() 链接起来?
我昨天刚刚从这个网站了解到我可以:
class Seq(object):
def __init__(self, seq):
self.seq = seq
def __repr__(self):
return repr(self.seq)
def __str__(self):
return str(self.seq)
def all(self):
return Seq(self.seq[:])
def head(self, count):
return Seq(self.seq[:count])
def tail(self, count):
return Seq(self.seq[-count:])
def odd(self):
return Seq(self.seq[1::2])
def even(self):
return Seq(self.seq[::2])
def reverse(self):
return Seq(self.seq[::-1])
>>> s = Seq(range(0, 100))
>>> print s.head(10).odd().even().reverse()
[9, 5, 1]
我想枚举 Seq 类中这些序列方法链的可能组合,可能是:
itertools.product([s.head,s.odd,s.even,s.reverse], repeat=4)
# may not just limited those 4 functions
如何使用
itertools.product()
至1).生成可调用的函数链列表?就像这样:
foo = s.head().odd().even().reverse()
2).生成可 eval() 的链字符串,然后我可以稍后将其存储在 ascii 文件或 eval() 中或用于记录目的?
<前><代码> head_lmt = [10,20,30] tail_lmt = [30,40,50] foo = s.head().odd().tail().reverse() ^------------------------------------head_lmt 10 或 20 或 30 ^-----------------------tail_lmt 30 或 40 或 50head()
、tail()
可以接受参数,even()
、odd()
> 不需要,例如, head() 和 tail() 的参数可以来自列表:
如果我的 Q1 是可能的,我如何将这些参数填充到可调用列表和 eval() 可字符串中,也就是生成更具体的可调用列表和可 eval() 的字符串?
谢谢!
I just learned yesterday from this site that I can:
class Seq(object):
def __init__(self, seq):
self.seq = seq
def __repr__(self):
return repr(self.seq)
def __str__(self):
return str(self.seq)
def all(self):
return Seq(self.seq[:])
def head(self, count):
return Seq(self.seq[:count])
def tail(self, count):
return Seq(self.seq[-count:])
def odd(self):
return Seq(self.seq[1::2])
def even(self):
return Seq(self.seq[::2])
def reverse(self):
return Seq(self.seq[::-1])
>>> s = Seq(range(0, 100))
>>> print s.head(10).odd().even().reverse()
[9, 5, 1]
I want to enumerate possible combination of those sequence method chains inside of class Seq, may sort of:
itertools.product([s.head,s.odd,s.even,s.reverse], repeat=4)
# may not just limited those 4 functions
how to use the
itertools.product()
to1). generate invoke-able function chains list? just like this:
foo = s.head().odd().even().reverse()
2). generate eval()able chain strings then I can store in ascii file or eval() later or for logging purpose?
the
head()
,tail()
may accept parameter, andeven()
,odd()
is not need to, for example,
the paremeter of head() and tail() may from lists:head_lmt = [10,20,30] tail_lmt = [30,40,50] foo = s.head().odd().tail().reverse() ^------------------------------------head_lmt 10 or 20 or 30 ^-----------------------tail_lmt 30 or 40 or 50
If my Q1 is possible, how I can fill those parameter into the invoke-able list and the eval()-able string, a.k.a generate more specific invoke-able list and the eval()-able string?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,类似“s.head()”的内容表示“绑定”到 Seq 的特定实例(即“s”)的方法。像“Seq.head()”这样的东西意味着一种未绑定的方法,因此仍然可以传递 Seq 的不同实例。
从那里开始,它只需要基本的函数组合和字符串连接。
不过,我不确定附加参数的示例是什么意思。您打算如何将不同的参数值与不同的功能组合结合起来?
Note that something like "s.head()" means a method which is "bound" to that specific instance of Seq, that is, "s." Something like "Seq.head()" means a method which is unbound, so one can still pass in different instances of Seq.
From there it simply requires basic functional composition and string concatenation.
I'm not sure what you mean by the examples for the additional parameters, though. How do you intend to combine the different parameter values with the different combinations of functions?