Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

As a Java programmer learning Python, what should I look out for? [closed]

Tags:

java

python

Much of my programming background is in Java, and I'm still doing most of my programming in Java. However, I'm starting to learn Python for some side projects at work, and I'd like to learn it as independent of my Java background as possible - i.e. I don't want to just program Java in Python. What are some things I should look out for?

A quick example - when looking through the Python tutorial, I came across the fact that defaulted mutable parameters of a function (such as a list) are persisted (remembered from call to call). This was counter-intuitive to me as a Java programmer and hard to get my head around. (See here and here if you don't understand the example.)

Someone also provided me with this list, which I found helpful, but short. Anyone have any other examples of how a Java programmer might tend to misuse Python...? Or things a Java programmer would falsely assume or have trouble understanding?

Edit: Ok, a brief overview of the reasons addressed by the article I linked to to prevent duplicates in the answers (as suggested by Bill the Lizard). (Please let me know if I make a mistake in phrasing, I've only just started with Python so I may not understand all the concepts fully. And a disclaimer - these are going to be very brief, so if you don't understand what it's getting at check out the link.)

  • A static method in Java does not translate to a Python classmethod
  • A switch statement in Java translates to a hash table in Python
  • Don't use XML
  • Getters and setters are evil (hey, I'm just quoting :) )
  • Code duplication is often a necessary evil in Java (e.g. method overloading), but not in Python

(And if you find this question at all interesting, check out the link anyway. :) It's quite good.)

like image 290
froadie Avatar asked Feb 26 '10 03:02

froadie


People also ask

Should Java developers learn Python?

Apart from all the other advantages, it is always beneficial to command multiple programming languages. Therefore, if you are a Java programmer, who is looking forward to learning a new language, now is the right time to learn Python.

Is it hard to learn Python after Java?

Java and Python are two of the most popular programming languages. Of the two, Java is the faster language, but Python is simpler and easier to learn. Each is well-established, platform-independent, and part of a large, supportive community. But that is where the similarities end.

Is it good to switch from Java to Python?

Java too provides an option for game development, but it's not as popular as Python in this domain. If you want to create some high-end graphics then Python is the best option for this as it provides you with all kinds of libraries and powerful engines. You can't develop a game entirely on Python or Java.


1 Answers

  • Don't put everything into classes. Python's built-in list and dictionaries will take you far.
  • Don't worry about keeping one class per module. Divide modules by purpose, not by class.
  • Use inheritance for behavior, not interfaces. Don't create an "Animal" class for "Dog" and "Cat" to inherit from, just so you can have a generic "make_sound" method.

Just do this:

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?" 
like image 178
Ryan Ginstrom Avatar answered Sep 21 '22 15:09

Ryan Ginstrom