ideas & tech in beta

 

Swapping 2 variable values in python

I have been using python a little recently to build some internal tools and am really really impressed. It is the nut crcker to the c# sledge hammer.

A community accepted web frame work seems to be the main thing lacking; many shops are rolling their own. I think this fragments the community and prevents python from competing effectivly with ruby/rails.

Django, which is the main player, seems to insist you use their orm which doesnt fit with our architecture or ideological commitments;)

Either way python has some geat features. For example one line variable swaping

a = 1
b = 2

a, b = b, a

You can swap the values of two variables without the use of a third variable and in one line.

In c# you can do this without the 3rd variable by using xor.

int a = 1;
int b = 2;

a ^= b;
b ^= a;
a ^= b;

Still three lines though; not nearly as elegant as the python solution.

This is a lovely little trick which gives a hint of how flexible and powerful python can be in more 'real' world uses.

python csharp programming code blog
Posted by: Harry McCarney
Last revised: 18 Jul, 2011 07:56 PM History

Comments

Your Comments

Used for your gravatar. Not required. Will not be public.
Posting code? Indent it by four spaces to make it look nice. Learn more about Markdown.

Preview