473,586 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a parameter to filter

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),r ange(20))
Traceback (most recent call last):
File "<pyshell#1 6>", line 1, in -toplevel-
filter(f(k=3),r ange(20))
TypeError: f() takes at least 1 non-keyword argument (0 given)

I next try filter(f(3),ran ge(20))
Traceback (most recent call last):
File "<pyshell#1 7>", line 1, in -toplevel-
filter(f(3),ran ge(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
Jul 18 '05 #1
2 4347
Thomas Philips wrote:
Now, I try to filter a range using
filter(f(k=3),r ange(20))


f(k=3) calls the function, so you're passing the return value to filter,
not the function itself. that's obviously not what you want.

some alternatives:

filter(lambda x: f(x, 3), range(20))

[x for x in range(20) if f(x, 3)]

the lambda form creates an "anonymous function" that takes one
argument, and calls your f() function with the right arguments.

the second form is a "list comprehesion", which is a compact way
to write for-in loops.

</F>

(this reply will be followed by 20 replies pointing you to 150-line scripts
that lets you do what you want by a combination of metaclasses, byte-
code rewriting, and iterators...)


Jul 18 '05 #2
"Fredrik Lundh" <fr*****@python ware.com> writes:
filter(lambda x: f(x, 3), range(20))

[x for x in range(20) if f(x, 3)]
</F>

(this reply will be followed by 20 replies pointing you to 150-line scripts
that lets you do what you want by a combination of metaclasses, byte-
code rewriting, and iterators...)


In your list you forgot the additional 20 replies talking about currying
(and the replies to them that currying is something different)

Thomas
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3099
by: andy | last post by:
i am coding a dll which contains a function take a const wchar_t* as parameter, call it form a exe, the wchar_t is passed to dll function, but the function in dll will then call another function and pass that wchar_t* parameter to another function, now the problem is that functions in dll, when exported function pass parameter another...
6
16258
by: wiredless | last post by:
What is the advantage of passing an interface type? according to UML (visio) when i reverse engineer existing code to a UML diagram I get the error "UMLE00046: sample : - An interface cannot be used as the type of a parameter." for this code:
2
1320
by: Khamal | last post by:
need to passing parameter from 1st page to 2nd page... try to use public static void user_param(string namaku ,string umurku ) but still cannot why?? please help... -- :: KHAMAL ::
1
1423
by: Joe Smith | last post by:
How to pass parameter to a Vb.Net application? ..Net Framework 1.1. Thanks, Joe
4
2750
by: Ranginald | last post by:
Hi, I'm having trouble passing a parameter from my default.aspx page to my default2.aspx page. I have values from a query in a list box and the goal is to pass the "catID" from default.aspx to a stored procedure on the details2.aspx page. I can successfully pass the values from the listbox control to a
1
18511
by: anushree1631 | last post by:
hi I am working in C#.Net 2005. I want the whole code for Passing Parameter in Crystal report Please send it fast It's very urgent with regards, Anushree
2
5035
by: Hakan Örnek | last post by:
Hi , I want to parameter passing to my windows sevice. I call service commands like this ; '------------------------------------------------------------ Dim sc As ServiceController sc = New ServiceController("ProsetLogServices") sc.MachineName = "." If sc.Status = ServiceControllerStatus.Stopped Then sc.Start() End If
1
1342
by: nabakishorebehera | last post by:
Hi All, I am Nabakishore, I am passing the parameter in a function as pointer,but i am passing that parameter as refernce in the definition. iam geting some warnig like function definition with out prototype. can u tell me what may be the problem.
1
5811
by: imtmub | last post by:
Hi All, I have mainpage (POselect.aspx). language VB In that page i have a textbox (txtPOId) and command button(Button1) When i press then button its should send the textbox value to Crystal report and open parameter. I have crystal report (CrystalReport1) that display the matched records what client entered in the textbox from main page. ...
7
5345
by: AmiMitra | last post by:
i am having problem with passing parameter through navigateUrl property of a hyperlink. in the page where i am using the hyperlink , i am getting the data thorugh commonutility. i want to pass this data to the next page through navigateUrl. plz help.
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.