50% off from Dreamhost hosting plan (that's 59.50$ less for full hosting) - use promo code HOSTING_PROMO during account cration on Dreamhost

Nowalijka - Śledzenie nowych aukcji allegro


Python versus Java

31 Aug 06

pythonEver wondered why there are so many people that program in Python, although Java (along with .NET) is the main enterprise language? You can find out in this post at bitworking.org.

Joe talks about the things (technical term!) that Python has and Java misses. Here is the list of those abstractions with my comments and ways of simulating (give me a feedback with your propositions):

Review of Garmin nüvi 360 GPS with bargain price

1. First-class functions
This is known from C++, where you can define function pointer that can be passed around as e.g. a parameter to other function.

2. Keyword parameters
OK, that's the one I would like to have in Java, it would add a better strictness to the language, now if you want to simulate this kind of behaviour you would have to create a class and add setters/getters.

3. Default parameters
I don't know why did Sun skip this while creating Java, it is a very useful abstraction. The simulations of this can be achieved by many overloaded methods/constructors.

  1. public class A {
  2.  
  3.   private int value;
  4.  
  5.   void calculate(int a) {
  6.     value = a;
  7.   }
  8.  
  9.   void calculate() {
  10.     calculate(10); // this is the default
  11.   }
  12. }

4. Tuples
5. Parallel assignment
6. Efficient multiple return values
Here I can't give any proposition how to simulate this in Java (at some point arrays might be used). A very nice functionality of Python (and some other languages), especially in conjunction with the next one...
That is, returning a tuple out of a method. Again some may try to simulate it using arrays.

7. Continuations
They are a way to save a function execution, and restore it at later time, it's as if you could jump out of a function during it's execution, and than, when by calling the function again, returning to the exact point where the function has been saved.

Here's an example to give you a better understanding of this. It is a function that will return values from the beginning to the end:

  1. def fun(begin, end):
  2.   i = begin
  3.   while i <end:
  4.     yield(i)
  5.     i = i + 1
  6.  
  7. f=fun(10,100)
  8. f.next()
  9. f.next()
  10. f.next()

Executing above code gives:
10
11
12

8. User-defined operators
That one was probably a mistake, I don't know a way to add a user-defined operator to Python (give me a hint if I am wrong)

9. Closures
In one of the previous posts I mentioned about proposition to add closures to Java.

10. Meta-programming
I haven't used much of this in Python, but it allows e.g. to assigning methods to classes on the fly. A nice functionality but I don't know how could it be used in a corporate environment, with more than 5 developers creating the code.