Skip to content

Generics are cool, but Python is cooler

My friend Valentino has posted on his blog about a sample of code with C# and generics and he rewrote the sample in Python ;)

Here the samples:

C# 2.0:
List integers = new List();
for(int i=1; i< =10; i++) integers.Add(i);
int sum;
integers.ForEach(delegate(int i){ sum+=i; });

vs

Python (basically any version):
sum([x for x in xrange(11)])

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • Facebook
  • Twitter
  • Google Bookmarks
  • FriendFeed
  • Google Buzz
  • HackerNews
  • Posterous
  • Reddit
  • Slashdot
  • Tumblr
  • Anonymous

    I hate to say it, but this works only in Python 2.3 or higher. In Python 2.2 and below you get “NameError: name ‘sum’ is not defined”. Still, Python’s cooler. :)

  • Lantash

    Why so complicated? sum(range(11)) works as well. Python rocks!