The Artima Developer Community
Sponsored Link

Python Answers Forum
stupid Python tricks

4 replies on 1 page. Most recent reply: Jul 14, 2004 7:39 AM by Andy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Merriodoc Brandybuck

Posts: 225
Nickname: brandybuck
Registered: Mar, 2003

stupid Python tricks Posted: Sep 8, 2003 10:40 AM
Reply to this message Reply
Advertisement
the following post talking about Haskell and the elegance of something like sum = map((+),numbers) in the Buzz at http://zephyrfalcon.org/weblog/arch_d7_2003_09_06.html#e331
got me thinking about "how would i do this?"

I tried various permutations of

sumfcn = lambda *args: 0.+(map(int,args))
and got various traceback messages. I found that you can do the following

eval('+'.join(map(str,(3,4,5,6))))

I'm not sure anybody would do this in real life, but it's just a rather neat python kind of thing that you could make more general like this

>>> sumfcn = lambda *args: eval('+'.join(map(str,args)))
>>> sumfcn(3,4,5,6,7)
25
>>> sumfcn(3,4,1)
8
>>> sumfcn("3","2")
5


I'm going to throw it in my collection of utility functions because, sometimes, you just need to sum a list of numbers.

Anybody got any stupid Python tricks they would like to share?


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: stupid Python tricks Posted: Sep 15, 2003 12:11 PM
Reply to this message Reply
I think it might be even simpler if you used reduce().

I did something a somewhat similar thing using eval() with environment variabes, where certain features could be configured with boolean logic, like breakfastEnabled='HAM and EGGS and not SPAM'. Then at runtime, the environment variables HAM, EGGS and SPAM are set to either 1 or 0, based on some other installation parameters. They are substituted into this string, then evaluated and that determines whether a specific feature is enabled. Python makes these kinds of things pretty easy to do.

Doug Winter

Posts: 3
Nickname: winjer
Registered: Oct, 2003

Re: stupid Python tricks Posted: Oct 5, 2003 10:17 AM
Reply to this message Reply
I think the most elegant pythonic equivalent would be:

reduce(lambda a,b: a+b, numbers)

Anand Pillai

Posts: 3
Nickname: mallucoder
Registered: Oct, 2003

Re: stupid Python tricks Posted: Oct 21, 2003 1:13 AM
Reply to this message Reply
Elegancy is a matter of personal taste and does not
have to do anything with how the code performs.

If you post this in comp.lang.python you will get
a dozen replies on how this performs less when
compared to using 'operator add()' etc.

I once posted a rather 'elegant' looking solution
for printing a multilist using lambda and reduce
(it had multiple lambdas' and reduces' and looked very
elegant to me!) and got chaffed by Alex Martelli on
how it performs worse when compared to his solution.

So I would advise you from my experience to skip
the 'elegancy' part of code since it does not have to do
anything with how the code works.

-Anand Pillai

Andy

Posts: 28
Nickname: adn
Registered: Jul, 2004

Re: stupid Python tricks Posted: Jul 14, 2004 7:39 AM
Reply to this message Reply
How about

>>> sumfcn = lambda *numbers : sum(numbers)
>>> sumfcn(1, 2, 3)
6

?

Flat View: This topic has 4 replies on 1 page
Topic: forget my other post, read this one Previous Topic   Next Topic Topic: Using socket over a firewall

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use