Programming in Python vs. programming in Java

后端 未结 13 1385
天命终不由人
天命终不由人 2020-12-23 09:45

I\'ve been writing Java for the last couple of years , and now I\'ve started to write in python (in addition).

The problem is that when I look at my Python code it

相关标签:
13条回答
  • 2020-12-23 10:32

    Learn a few other languages. It will help you make the difference between algorithms (the structure of processing, unchanged between languages) and the local syntaxic features of the language. Then you can "write Foo in Bar" for any combination of languages "Foo" and "Bar".

    0 讨论(0)
  • 2020-12-23 10:34

    Some of the major ways in which Python differs from C/Java-like languages are:

    1. List comprehensions.

    2. Support for functional programming.

    3. The use of certain Pythonic constructs instead of similar C-like constructs although both seem to work (list comprehensions can be argued to be a part of this, but there are others).

    There are others, but these are the main ones that bugged me when I first started Python (and I had come from years of Java like you).

    Before using any of these, it is helpful to understand why you should go for pythonic code rather than the usual C/Java way in Python, although both give you the same output.

    For starters, Python provides some powerful features not available in C/Java that makes your code much clearer and simpler (although this is subjective, and might not look any better to someone coming from Java at first). The first two points fall into this category. For example, support for functions as first class objects and closures makes it easy to do things that would need all kinds of weird acrobatics with inner classes in Java.

    But a major reason is that Python is an interpreted language, and certain constructs are much faster than the equivalent C/Java-like code. For example, list comprehensions are usually a lot faster than an equivalent for-loop that iterates over the indices of a list and accesses each item by index. This is a very objective benefit, and IMHO a lot of the "Python in way too slow" way of thinking comes from using Java-style code shoe-horned into Python.

    One of the best ways to learn about pythonic code is to read other people's code. I actually learnt a lot by looking at Python code posted in answers to SO questions. These often come with explanations and it is usually obvious why it is better than non-pythonic code (speed, clarity, etc.).

    Edit:

    Of course, there are other ways of getting other people's code. You can also download and look through the code of any good open source Python project. Books are also a good resource, I would recommend O'Reilly Python Cookbook. It has lots of useful code examples and very detailed explanations.

    0 讨论(0)
  • 2020-12-23 10:35

    You could start by reading The Zen of Python. It'll give you some insight into how Python code is supposed to be written, provided you understand the language enough to understand what it's talking about. :-)

    0 讨论(0)
  • 2020-12-23 10:35

    Eat Python, Sleep Python and Drink Python. That is the only way........

    0 讨论(0)
  • 2020-12-23 10:39

    Try to find algorithms that you understand well and see how they are implemented in python standard libraries.

    Persist. :)

    0 讨论(0)
  • 2020-12-23 10:42

    If you want to see some fairly idiomatic Python that does non-trivial stuff, there's Dive Into Python, although Dive Into Python 3 is newer and might be a better source of style tips. If you're looking more for some points to review, there's Code Like a Pythonista.

    0 讨论(0)
提交回复
热议问题