Passing Multiple Arguments To Pool.map Using Class Function
I'm trying to thread as described in this post, and also pass multiple arguments in Python 2.7 through a work-around described here. Right now I have something like this, a functi
Solution 1:
itertools.izip(username*len(self.repo_list),itertools.repeat(self.repo_list))
yields a tuple
.
You need to pass 2 arguments explicitly to your method (self
is implicitly passed because it's a non-static method), but you only pass 1 tuple explicitly, plus the implicit self
which makes 2 arguments, hence the confusing error message.
You have to use *
to pass your tuple as 2 separate arguments, like this:
master_list = pool.map(self.length_scraper2,
*itertools.izip(username*len(self.repo_list),itertools.repeat(self.repo_list)))
simple test using the classical map
on a simple function:
def function(b,c):
return (b,c)
print(list(map(function,zip([1,2],[4,5]))))
error:
print(list(map(function,zip([1,2],[4,5]))))
TypeError: function() missing 1 required positional argument: 'c'
now adding single asterisk to expand args:
print(list(map(function,*zip([1,2],[4,5]))))
works:
[(1, 2), (4, 5)]
same goes for class method:
classFoo:
deffunction(self,b,c):
return (b,c)
f = Foo()
print(list(map(f.function,*zip([1,2],[4,5]))))
Post a Comment for "Passing Multiple Arguments To Pool.map Using Class Function"