Re: Dynamic comparison operators

2012-05-25 Thread Jon Clements
> > Any time you find yourself thinking that you want to use eval to solve a > problem, take a long, cold shower until the urge goes away. > > If you have to ask why eval is dangerous, then you don't know enough > about programming to use it safely. Scrub it out of your life until you > have l

Re: Dynamic comparison operators

2012-05-24 Thread Paul Rubin
mlangenho...@gmail.com writes: > I would like to pass something like this into a function > test(val1,val2,'>=') > > and it should come back with True or False. import operator test(val1, val2, operator.ge) -- http://mail.python.org/mailman/listinfo/python-list

Re: Dynamic comparison operators

2012-05-24 Thread Steven D'Aprano
On Thu, 24 May 2012 11:22:37 -0400, Colin J. Williams wrote: > On 24/05/2012 10:14 AM, mlangenho...@gmail.com wrote: >> I would like to pass something like this into a function >> test(val1,val2,'>=') >> >> and it should come back with True or False. >> >> Is there a way to dynamically compare 2 v

Re: Dynamic comparison operators

2012-05-24 Thread Colin J. Williams
On 24/05/2012 10:14 AM, mlangenho...@gmail.com wrote: I would like to pass something like this into a function test(val1,val2,'>=') and it should come back with True or False. Is there a way to dynamically compare 2 values like this or will I have to code each operator individually? Would so

Re: Dynamic comparison operators

2012-05-24 Thread Jussi Piitulainen
Alain Ketterlin writes: > mlangenho...@gmail.com writes: > > > I would like to pass something like this into a function > > test(val1,val2,'>=') > > > > and it should come back with True or False. > > def test(x,y,c): > return c(x,y) > > Call with: test(v1,v2, lambda x,y:x<=y ). A bit noisy

Re: Dynamic comparison operators

2012-05-24 Thread Tim Chase
On 05/24/12 09:32, Phil Le Bienheureux wrote: >> I would like to pass something like this into a function >> test(val1,val2,'>=') > > You can pass an operator as an argument to your function. > > See : > http://docs.python.org/library/operator.html And if you want to use strings, you can map them

Re: Dynamic comparison operators

2012-05-24 Thread Alain Ketterlin
mlangenho...@gmail.com writes: > I would like to pass something like this into a function > test(val1,val2,'>=') > > and it should come back with True or False. def test(x,y,c): return c(x,y) Call with: test(v1,v2, lambda x,y:x<=y ). A bit noisy imho. If you have a finite number of comparis

Re: Dynamic comparison operators

2012-05-24 Thread Phil Le Bienheureux
Hello, You can pass an operator as an argument to your function. See : http://docs.python.org/library/operator.html Regards, -- Forwarded message -- From: Date: 2012/5/24 Subject: Dynamic comparison operators To: python-list@python.org I would like to pass something like thi