Passing a parameter to filter
Thomas Philips
tkpmep at hotmail.com
Fri Apr 16 12:53:24 EDT 2004
To experiment with filtering, I define a function f(x,k) as follows
>>> def f(x,k=2):
return x%k==0
I can check that it works by typing
>>> f(10,3)
False
Now, I try to filter a range using
>>> filter(f(k=3),range(20))
Traceback (most recent call last):
File "<pyshell#16>", line 1, in -toplevel-
filter(f(k=3),range(20))
TypeError: f() takes at least 1 non-keyword argument (0 given)
I next try
>>> filter(f(3),range(20))
Traceback (most recent call last):
File "<pyshell#17>", line 1, in -toplevel-
filter(f(3),range(20))
TypeError: 'bool' object is not callable
But, as k defaults to 2, I get exactly what I expect from
>>> filter(f,range(20))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>>
What's wrong with my syntax when passing the parameter k=3?
Sincerely
Thomas Philips
More information about the Python-list
mailing list