COM pressing" N" -time对象的成员通话 [英] Compressing "n"-time object member call
问题描述
有任何非显性为
办法调用成员 N
倍于一个对象?
Is there any non-explicit for
way to call a member n
times upon an object?
我在想一些的map / reduce /λ
的办法,但我不能想出一个办法做到这一点 - 如果可能的话
I was thinking about some map/reduce/lambda
approach, but I couldn't figure out a way to do this -- if it's possible.
我想补充的背景下,我使用 BeautifulSoup
,而且我提取HTML表格中的一些元素;我提取一些元素,然后在最后一个。
Just to add context, I'm using BeautifulSoup
, and I'm extracting some elements from an html table; I extract some elements, and then, the last one.
因为我有:
# First value
print value.text
# Second value
value = value.nextSibling
print value.text
# Ninth value
for i in xrange(1, 7):
value = value.nextSibling
print value.text
我想知道是否有任何的λ
办法 - 或者别的东西 - 这将允许我这样做:
I was wondering if there's any lambda
approach -- or something else -- that would allow me to do this:
# Ninth value
((value = value.nextSibling) for i in xrange(1, 7))
print value.text
PS:没有,任何没有问题与为
的办法,但我真的很喜欢单行的解决方案,而这将适合我的code真的很好
P.S.: No, there's no problem whatsoever with the for
approach, except I really enjoy one-liner solutions, and this would fit really nice in my code.
推荐答案
我对循环强烈preference,但你的可能的使用减少
:
I have a strong preference for the loop, but you could use reduce
:
>>> class Foo(object):
... def __init__(self):
... self.count = 0
... def callme(self):
... self.count += 1
... return self
...
>>> a = Foo()
>>> reduce(lambda x,y:x.callme(),range(7),a)
<__main__.Foo object at 0xec390>
>>> a.count
7
这篇关于COM pressing&QUOT; N&QUOT; -time对象的成员通话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!