简体   繁体   English

如何在python中将参数传递给callable-iterator函数?

[英]How to pass arguments to function of callable-iterator in python?

Python 2.6.2 Python 2.6.2

>>> call_iter = iter(lambda x: x + 1, 100)
>>> call_iter.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 1 argument (0 given)

I want to pass argument to lambda x:x + 1 . 我想将参数传递给lambda x:x + 1

Update: I think the example above is hard to understand. 更新:我认为上面的例子很难理解。

I wonder whether there is a builtin func like myiter in python: 我想知道是否在python中有像myiter这样的内置函数:

class myiter:
    def __init__(self, callable, initial, sentinel):
        self.value = initial
        self.callable = callable
        self.sentinel = sentinel

    def __iter__(self):
        return self

    def next(self):
        if self.value == self.sentinel:
            raise StopIteration
        else:
            # calculate next value from prev value
            self.value = self.callable(self.value) 
            return self.value

if __name__ == '__main__':
    call_iter = myiter(lambda x:x + 1, 0, 100)
    for i in call_iter:
        print i

I'm not sure what you are trying to accomplish here, but 我不确定您要在这里完成什么,但是

>>> call_iter = iter(lambda:lambda x: x + 1, 100)
>>> next(call_iter)(1)
2

The form of iter you're trying to use only takes a 0-argument function. 您尝试使用的iter形式仅采用0参数函数。 The below is for illustration only; 以下仅用于说明; don't actually do this. 实际上不这样做。

>>> x = 0
>>> def counter():
...     global x
...     x += 1
...     return x
... 
>>> list(iter(counter, 10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

In general, this form of iter isn't very useful. 通常,这种iter形式不是很有用。 It requires some kind of callable that maintains state between calls. 它需要某种可调用的方法来维护两次调用之间的状态。 So for example, you could pass the readline method of a file object, as suggested in the docs . 因此,例如,您可以按照docs中的建议传递文件对象的readline方法。 But generally, there are better ways to do this. 但是通常,有更好的方法可以做到这一点。 So for example, say you created a class like this: 例如,假设您创建了一个这样的类:

>>> class Incrementer(object):
...     def __init__(self):
...         self.state = 0
...     def __call__(self):
...         rval = self.state
...         self.state += 1
...         return rval
... 
>>> list(iter(Incrementer(), 10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

It's lovely and all, but if you have to create a class that's supposed to be iterable, you might as well make it a real iterator by giving it a next method and an __iter__ method. 它很可爱,但是,如果您必须创建一个应该是可迭代的类,则可以通过给它一个next方法和一个__iter__方法来使其成为真正的迭代器。 Conversely, if you aren't creating a class, just use yield 相反,如果您不创建类,则使用yield

i think what you want is: 我想你想要的是:

call_iter = iter(map(lambda x: x + 1, range(100)))
>>> call_iter.next()
1
>>> call_iter.next()
2
>>> call_iter.next()
3
>>> 

to pass an argument to the lambda function you need to map the lambda to an iterable like range(100) or [2,4,5] 要将参数传递给lambda函数,您需要将lambda映射到类似range(100)或[2,4,5]的可迭代对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM