Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of things can be done with Java but not Python?

I would to pick up a new programming language - Java, having been using Python for some time. But it seems most things that can be done with Java can be done with Python. So I would like to know

  1. What kind of things can be done with Java but not Python?
    • mobile programming (Android).
    • POSIX Threads Programming.
  2. Conversely, What kind of things can be done with Python but not Java if any?

clarification: I hope to get an answer from a practical point of view but not a theoretical point of view and it should be about the current status, not future. So theoretically all programming languages can perform any task, practically each is limited in some way.

like image 742
kakarukeys Avatar asked Feb 26 '11 10:02

kakarukeys


2 Answers

Both languages are Turing complete, both have vast libraries, and both support extensions written in C so that you can access low level code if needed. The main difference is where they are currently supported. Java in general has wider support than Python.

Your example of Android is one place where Java is the standard choice, although Python also has some support in the form of Android Scripting Environment. Java is already installed on most home computers. You can write Java applets and expect them to work in most browsers.

One thing you can't easily do in Java is quickly write short scripts that perform useful tasks. Python is more suitable for scripting than Java (although there are of course other alternatives too).

like image 77
Mark Byers Avatar answered Oct 11 '22 13:10

Mark Byers


I guess using Jython, you can do anything with Python that you can do in Java.

Conversely, Python has the PyPy compiler, which is pretty cool - a virtual machine with multiple backeds (Java Runtime, LLVM, .net, and Python IIRC), multiple garbage collectors, multiple implementations (Stackless), etc. I know Java has a big choice of virtual machines, but the growth of PyPy is amazing, due to it being written in RPython - a fairly productive language.

Also, can a Java do this, in 1 file and less that 20 lines, with no library imports? Obviously both languages have libraries that can do this, but I'm just talking about the flexibility of the languages.

class Logger(object): # boilerplate code
    def log(self,level,msg,*args,**kwargs): # *args, **kwargs = flexible arguments
        self._log(level,msg,*args,**kwargs) # call with flexible argments

    def _log(self,level,msg,*args,**kwargs):
        # override me at runtime :) 
        # I think Java people call this Dependency Runtime Injection
        if level>1:
            print msg,args,kwargs

logger = Logger() # boilerplate code

def logged(level): # what pattern do you call this?
    def logged_decorator(function): # and this? 
        def func(*args,**kwars): 
            name = func.__name__ # look ma, reflective metaprogramming!
            logger.log(level,name,*args,**kwargs)
            return func(*args,**kwargs)
        return func # boilerplate code
    return logged_decorator # boilerplate code

Example use:

@logged
def my_func(arg1,arg2):
   # your code here
   pass
like image 24
wisty Avatar answered Oct 11 '22 13:10

wisty