哪些事情可以用 Java 而不是 Python?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5126346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What kind of things can be done with Java but not Python?
提问by kakarukeys
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
我会选择一种新的编程语言 - Java,使用 Python 已经有一段时间了。但是似乎可以用 Java 完成的大多数事情都可以用 Python 完成。所以我想知道
- What kind of things can be done with Java but not Python?
- mobile programming (Android).
- POSIX Threads Programming.
- Conversely, What kind of things can be done with Python but not Java if any?
- 哪些事情可以用 Java 而不是 Python?
- 移动编程(安卓)。
- POSIX 线程编程。
- 相反,如果有的话,Python 可以做哪些事情而不是 Java 呢?
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.
澄清:我希望从实践的角度而不是理论的角度得到答案,应该是关于现状,而不是未来。所以理论上所有编程语言都可以执行任何任务,实际上每种语言都在某种程度上受到限制。
采纳答案by wisty
I guess using Jython, you can do anything with Python that you can do in Java.
我想使用 Jython,你可以用 Python 做任何你可以用 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.
相反,Python 有 PyPy 编译器,它非常酷——一个具有多个支持(Java 运行时、LLVM、.net 和 Python IIRC)、多个垃圾收集器、多个实现(Stackless)等的虚拟机。我知道 Java 有一个虚拟机的大量选择,但 PyPy 的增长是惊人的,因为它是用 RPython 编写的——一种相当高效的语言。
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.
此外,Java 可以在 1 个文件和少于 20 行的文件中执行此操作,而无需导入库吗?显然这两种语言都有可以做到这一点的库,但我只是在谈论语言的灵活性。
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
回答by Mark Byers
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.
这两种语言都是图灵完备的,都拥有庞大的库,并且都支持用 C 编写的扩展,以便您可以在需要时访问低级代码。主要区别在于它们目前在何处受支持。Java 通常比 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.
您的 Android 示例是 Java 是标准选择的一个地方,尽管 Python 也以Android Scripting Environment的形式提供了一些支持。大多数家用计算机上已经安装了 Java。您可以编写 Java 小程序并期望它们在大多数浏览器中工作。
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).
在 Java 中无法轻松完成的一件事是快速编写执行有用任务的简短脚本。Python 比 Java 更适合编写脚本(当然也有其他替代方案)。
回答by sgokhales
You would surely lovereading the comparisons made below between these 2 languages.
Check them :
您一定会喜欢阅读下面这两种语言之间的比较。
检查它们:
回答by Tony Merrit
CPython has a lot of libraries with bindings to native libraries--not so Java.
CPython 有很多与本机库绑定的库——Java 则不然。