As a Java programmer learning Python, what should I look for?

Most of my programming background is in Java, and I still run most of my Java programs. However, I am starting to learn Python for some side projects at work, and I would like to learn it as independent of my Java background as possible - that is, I don't want to just program Java in Python. What things should I look for?

A quick example - when I looked at a Python tutorial, I came across the fact that changed function parameters (such as a list) are saved (remembered when called on a call). This contradicted me as a Java programmer, and it was hard for me to plunge. (See here and here if you do not understand the example.)

Someone also provided me with this list, which I found useful, but short. Does anyone have other examples of how a Java programmer can misuse Python ...? Or are things that a Java programmer mistakenly suspected or had trouble understanding?

Edit : Well, a brief overview of the reasons raised in Article I related to preventing duplicate responses (as suggested by Bill Lizard). (Please let me know if I am mistaken in the wording, I just started with Python, so I can’t fully understand all the concepts. And the disclaimer will be very brief, so if you don’t understand what it gets to check link.)

  • Static method in Java does not translate to Python class class
  • The switch statement in Java translates into a hash table in Python
  • Do not use XML
  • Getters and setters are evil (hey, I'm just quoting :))
  • Copying duplication is often a necessary evil in Java (e.g. method overloading), but not in Python

(And if you find this question generally interesting, check out the link anyway. :) That's pretty good.)

+62
java python
Feb 26 '10 at 3:48 on
source share
7 answers
  • Do not put everything in classes . The built-in Python list and dictionaries will take you far.
  • Do not worry about saving one class in a module . Separate modules according to purpose, not class.
  • Use inheritance for behavior, not interfaces . Do not create an “Animal” class for “Dog” and “Cat” for inheritance, so you can have a common make_sound method.

Just do the following:

class Dog(object): def make_sound(self): return "woof!" class Cat(object): def make_sound(self): return "meow!" class LolCat(object): def make_sound(self): return "i can has cheezburger?" 
+24
Feb 26 '10 at 4:00
source share

There are some helpful tips in this article that can be easily interpreted and misunderstood. And some bad tips.

Leave Java behind. Start fresh. "Don't trust your [Java-based] instincts." Saying things "contrary to intuition" is a bad habit in any programming discipline. When you learn a new language, start all over again and give up your habits. Your intuition must be wrong.

Languages ​​are different. Otherwise, they would be in the same language with different syntax, and would be simple translators. Since there are no simple translators, there is no simple comparison. This means that intuition is useless and dangerous.

  • "The static method in Java does not translate into a class of the Python class." Such things are really limited and useless. Python has a staticmethod decorator. It also has a classmethod decorator for which Java has no equivalent.

    This point, BTW, also included much more useful tips on how not to impose all things in the class. "The idiomatic translation of a static Java method is usually a module level function."

  • The Java switch in Java can be implemented in several ways. First of all, this is usually the construction of if elif elif elif . In this regard, the article is useless. If you are absolutely sure that this is too slow (and can prove it), you can use the Python dictionary as a slightly faster mapping from value to block of code. Blindly translating a dictionary (without thinking) is really bad advice.

  • Do not use XML. It makes no sense when taken out of context. In context, this means that you should not rely on XML to add flexibility. Java relies on a description of the material in XML; WSDL files, for example, repeat information that is obvious when checking code. Python relies on introspection instead of translating everything into XML.

    But Python has great XML processing libraries. Some.

  • Getters and seters are not required in Python as they are required in Java. Firstly, you have the best introspection in Python, so you don't need getters and setters to create dynamic bean objects. (For this you use collections.namedtuple ).

    However, you do have a property decorator that associates getters (and setters) with a construct similar to an attribute. The fact is that Python prefers naked attributes; if necessary, we can connect getters and setters so that they look as if there is a simple attribute.

    Python also has descriptor classes if the properties are not complex enough.

  • Repeating code is often a necessary evil in Java (e.g. method overloading), but not in Python. Right. Python uses optional arguments instead of method overloading.

    The bullet point spoke of closure; this isn’t as useful as a simple tip for using the default argument values ​​wisely.

+22
Feb 26 '10 at 11:35
source share

One thing you could use in Java that you won't find in Python is strict confidentiality. This is not much to look for, since it does not need to be searched (I'm confused by how long I searched for the Python equivalent for 'private' when I started!). Instead, Python has much more transparency and easier introspection than Java. This falls under what is sometimes called the philosophy of "we all agree with adults." There are several conventions and language mechanisms that help prevent the inadvertent use of "non-public" methods, etc., but the whole image of hiding information is practically absent in Python.

+13
Feb 26 '10 at 4:51
source share

The biggest that I can think of is not understanding or making full use of duck print. In Java, you need to provide very explicit and detailed type information up. In Python, typing is dynamic and largely implicit. The philosophy is that you should think about your program at a higher level than the nominal ones. For example, in Python, you do not use inheritance to replace the model. Replaceability appears by default as a result of duck printing. Inheritance is just the convenience of a programmer for reusing an implementation.

Similarly, the Python idiom is "ask for forgiveness, do not ask permission." Explicit typing is considered evil. Do not check if parameter has specific type of upfront. Just try to do everything you need with the parameter. If it does not match the correct interface, it will throw a very clear exception, and you can quickly find the problem. If someone passes a type parameter that was unexpectedly unexpected but has the same interface as you expected, then you got the flexibility for free.

+9
Feb 26 2018-10-02T00
source share

The most important thing from Java POV is that it’s great not to do classes for everything. There are many situations where the procedural approach is simpler and shorter.

The next most important thing is that you will need to understand that the type of object controls what it can do; rather, the code controls which objects should be able to support at run time (this is due to duck input).

Oh, and use your own lists and dicts whenever possible (not configured descendants).

+6
Feb 26 '10 at 6:52
source share

The exceptions handled in Python differ from how they are handled in Java. Although the advice in Java is to use exceptions only for exceptional conditions, this is not so with Python.

In Python, things like Iterator use an exception mechanism to signal that there are no more elements. But such a design is not considered good practice in Java.

As Alex Martelli says in his Python book in a nutshell, the exclusion mechanism with other languages ​​(and applies to Java) LBYL (Look before the jump): consists of checking in advance, before attempting an operation, under all circumstances that may render the operation invalid.

Where, as with Python, the approach is EAFP (easier to apologize than permission)

+5
Feb 26 '10 at 10:30
source share

Narrative to "Don't use classes for everything": callbacks.

The Java method for making callbacks is based on passing objects that implement the callback interface (for example, ActionListener using the actionPerformed() method). In Python, nothing of the kind is required; you can directly pass methods or even locally defined functions:

 def handler(): print("click!") button.onclick(handler) 

Or even lambda:

 button.onclick(lambda: print("click!\n")) 
+1
Feb 26 '10 at 10:37
source share



All Articles