#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    9
    Rep Power
    0

    Python vs Java ?????


    Hello !

    I'm a studient and i have been researching on Python. My project is releated to programing web/internet with open source . At the beginning , i had decided to choose java - OOP language. Java has been famous as a power programing language , everyone knows this. But recently i have heard about Python-OOP language ,and i have read some articles about Python , i like it. But i really don't know how it is more power than Java (Some companies have said that :" Python is more power than java "). Can you show me some main ideas about this compare ( Python VS Java , how Python is
    more power than java ?) . Or you can show me some advantages of Python .
    If you think Python is'nt more power than Java , can you show me your reasons ? Please , help me.
    Thanks so much.
  2. #2
  3. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    19
    Well I know Java programmers who, after workimg with Java on real-world projecvts say that Python's standard library is better than Java's, but I am not sure about that. Both Java and Python are slow, but Python is only interpreted, whereas in Java you need to compile code to get the bytecodes and then interpret the bytecodes, so that process is a bit more complicated. On the other hand, Java is closed source and Python is open source.

    Here's a link to look at:http://www.ferg.org/projects/python_...e-by-side.html
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Firstly to correct Boki, both Java and Python compile down to a bytecode which is then interpreted, but in Java it has to be done as a separate step, while with Python it is done automatically.

    There are several reasons why Python is considered more powerful than Java.

    Firstly it has a higher level of abstraction, with many data structures as built in types, and many operations built into the language that would need to be done through a library in Java.

    For example here is a simple hello world program in Java and Python

    Code:
    #Python:
    print "hello world"
    Code:
    //Java:
    
    public class Hello {
       public static void main(String [] args) {
          System.out.println("hello world");
       }
    }
    (My Java is a bit rusty and I have not tested the code, so there may be errors in it).

    And here is something a bit more complex, creating a dictionary and then printing out the key/value pairs in it:

    Code:
    #Python
    myDict = { 1: 'one', 2: 'two', 3: 'three' }
    
    for key, val in myDict.items():
      print key, val
    Code:
    //java
    
    import java.util.*;
    
    public class Dict 
    {
        public static void main(String [] args) 
        {
                Hashmap myDict = new Hashmap();
                myDict.put(new Integer(1), "one");
                myDict.put(new Integer(2), "two");
                myDict.put(new Integer(3), "three");
                
                Iterator keyIterator = myDict.keySet().iterator();
                while ( keyIterator.hasNext() )
                {
                    Integer key = keyIterator.next();
                    String val = myDict.get(key);
                    System.out.println( key.ToString() + " " + val );
                }
        }
    }
    Secondly, while Python fully supports object oriented programming, it does not force you to use it. Notice the lack of class declarations in the previous examples, while Java insists on you creating classes, even if they are never used. Python on the other hand also supports functional and procedural programming, so you can use the best tool for the job.

    Thirdly, Python is dynamically typed. Notice in the previous Java examples that you have to declare the types of every variable. In Python a variable can take any type of object, so you do not have to declare it. Python objects are also dynamic - you can add and remove members on the fly.

    Fourthly, list comprehensions and generators. I do not have space here to explain them, but they enable you to do in a few lines things that Java programmers would not even consider attempting.

    Fifthly, the Python interactive environment. This enables you to try out bits of Python code on the fly and immediately see the results.

    Sixthly, in Python functions and methods are first class objects - you can pass a function to another function, or store it in a variable to be called later. In Java you have to muck about writing interfaces and creating anonymous inner classes - complex code to do something very simple. In Python you can also create a function inside another function and return it, with simple closures. e.g.

    Code:
    >>> # python code run at the interactive console
    >>> # create a function that returns a function that adds the number that you passed 
    >>> #to the first function to whatever you pass to the second function.  Phew!
    >>> def makeAdder(x):
    ... 	def add(n):
    ... 		return n+x
    ... 	return add
    ... 
    >>> add10 = makeAdder(10)
    >>> add10
    <function add at 0x00DDAEB0>
    >>> add10(3)
    13
    Code:
    //I have no idea how you would do this in Java!
    So in summary, Python allows you to create programs that are much shorter and more readable than in Java, and so development is much quicker.

    Dave - The Developers' Coach
    Last edited by DevCoach; September 15th, 2004 at 04:15 PM.
  6. #4
  7. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    19
    Originally Posted by DevCoach
    Firstly to correct Boki, both Java and Python compile down to a bytecode which is then interpreted, but in Java it has to be done as a separate step, while with Python it is done automatically.
    Yep, I overlooked that, however my point was that in Python all that at least appears to be simpler, less work for the programmer to do.
    As for the shortness of Python programs, Python's core is small, but powerfull. Even in Jython, an implementation of Python on Java platform, coding is sometimes 30-40% less than the one for the same applications in Java. Python is great because it allows you to focus on writing programs right away, without having to learn too much syntax, That's why IMO it is a great language to start with.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    9
    Rep Power
    0
    thank you so much ,special thank DevCoach.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    9
    Rep Power
    0
    Fourthly, list comprehensions and generators. I do not have space here to explain them, but they enable you to do in a few lines things that Java programmers would not even consider attempting.



    Can you tell me more , thanks.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by my_venus
    Fourthly, list comprehensions and generators. I do not have space here to explain them, but they enable you to do in a few lines things that Java programmers would not even consider attempting.



    Can you tell me more , thanks.
    Sure.

    List comprehensions enable you to create a list directly from an expression. the syntax is an expression using 'for' and 'if' statements inside square brackets.

    For example, here is a list comprehension that creates a list of the squares of all the numbers from 0 to 20 that are divisible by 3:

    Code:
    >>> [x*x for x in range(20) if (x%3) == 0]
    [0, 9, 36, 81, 144, 225, 324]
    >>>
    This is a shortcut for

    Code:
    list =[]
    for x in range(20):
      if (x % 3) == 0:
        list.append(x)
    While list comprehensions are just a shorthand way of expressing a loop that creates a list, generators are a new kind of function that create an iterator. A generator can be thought of as a function that can return a value, then carry on from where it left off. It uses the 'yield' keyword instead of 'return' to return a value.

    Here is a generator that returns a sequence of squares of numbers that are a multiple of the starting parameter:

    Code:
    >>> def squareMultiples(n):
    ... 	current = 0
    ... 	while 1:
    ... 		yield current*current
    ... 		current += n
    ...

    So squareMultiples(3) will return an iterator that creates the squares of the multiples of three. However unlike the list comprehension above the generator creates an iterator into an infinite sequence, so the code that uses it will need to limit how often it iterates:

    Code:
    >>> for x in squareMultiples(3):
    ... 	if x > 1000: break
    ... 	print x,
    ... 
     0 9 36 81 144 225 324 441 576 729 900
    Of course generators and list comprehensions are not limited to mathematical expressions. Here is a generator that acts as a filter on a sequence of strings, only returning strings that contain the given matchString:

    Code:
    >>> def findStrings(matchStr, sequence):
    ... 	for s in sequence:
    ... 		if matchStr in s:
    ... 			yield s
    ...
    And since file objects are iterable in Python, we can use this as a simple grep:

    Code:
    >>> classes = [ line for line in findStrings('class', open('myfile.py') ]
    this will create a list of all the lines in 'myfile.py' that contain the string 'class'.

    In Python 2.4 (currently in alpha) you can also have generator expressions, which are similar to list comprehensions but create a generator instead of a list. So for example to caculate the sum of the squares of the odd numbers from 1 to 100:

    Code:
    >>> sum(x*x for x in range(100) if x%2 == 1)
    166650
    Dave - The Developers' Coach
    Last edited by DevCoach; October 2nd, 2004 at 04:54 PM.

IMN logo majestic logo threadwatch logo seochat tools logo