I started with ActionScript 2.0 and then went on with Java. I have learned, or at least used, a bunch of languages since then, including Python (probably my favorite).

I'm afraid that my style of object oriented programming is very unpythonic though, and more like Java OOP with Python syntax. What makes Java like and Pythonic OOP differ from each other? What things do Java programmers often do "unpythonically" when writing object oriented code in Python?


For a Java guy Python is an anarchic playgound where anyone can grab a club and start mauling your head.

For a Python guy Java is a an Orwellian universe where you are constantly shackled to someone else`s diminishing view of how the universe tick.

The truth is anything you can do in one language you can do in the other just as cleanly. However as you have mentioned there are important difference in both community as to what clean means.

Java way : A clean system is one that does what is meant to and nothing else, it will not allow extensions or modifications that go against the nature of the intended purpose and will attempt to enforce these as much as possible through the compiler. Flexibility is obtained through careful crafting of simple interfaces within strict structures. In Java one`s sand box should always be clearly bounded and overstepping these met with swift feedback from the compiler. Java provides means to statically define object structures and create dynamic interactions from instances of them. When I work in Java I try to cleverly create basic building blocks towards a brain dead solution. I mostly work bottom-up once I have a working theory on how to tackle the problem.

Java will tend to produce large software that can span large teams and provides tools and means to keep the flock in check. If kept unchecked this will lead to very detached teams working independently towards an ever more unclear goal. Eventually each team becomes it`s own "raison d'être" and the system as a whole becomes diluted driving astray the main project. These can lead to extreme cost overruns and huge software systems that perform and maintain poorly.

There is almost never a small quick and easy way to do things in Java, but the IDE and the tooling are there to make painful tasks a mere few clicks away.

Python way: Clean means concise and easily readable. A good python system is designed to let you get right to the heart of it and exposes it`s innermost secrets in a way that you can understand from the code the intended use and purpose of it. It will also allow you to design your own solution around by extending and/or encapsulating the original design so that it will go exactly in your direction. Python provides means to create object templates from which you can dynamically change the instance to fit the needs at hand. In python I tend to tackle the problem right away and then spread the code in a logical structure such that the final solution remains as simple and readable as can be. In python I tend to work top-down and manage the increase complexities through a divide-and-conquer approach.

Python teams will tend to produce light systems and be very fast in delivering a working solution. They will tend to be a close knit bunch working interchangeably on any part of the system validating each other's solution every chance they get. They feed on each other creating a synergy that is quite exhilarating. However this creates teams that are difficult to scale to larger systems and often hit a sort of glass ceiling. Introducing new members in the team will help but it will take some time for the knowledge to spread around enough for the extra productivity to be felt. The team then becomes divided and the constant overview over the whole system dilutes as does the atmosphere of the early days. This can lead to overly convoluted code to what once was a simple problem, extreme cost overruns and systems that perform and maintain poorly.

There is almost always a quick and easy way to do things with Python but complexity can be harder to keep in check once the system reach a certain threshold.

In short, both have a dark side and both have clear strength. However, when prodding along both communities you will find that the strength of one leads to the dark side of the other and vice versa.

Hence the heated debates as to which is best.


So you know that whole thing about setting the visibility of methods and variables? Yeah, those don't exist anymore, everything is public. There are naming conventions and name mangling, but everything is really still available.

Part of the flexibility of Python comes from that fact that you are allowed to do almost anything. Because of that, the philosophy is that people should know how to use the API rather than the API enforcing that a method is used properly.

Instead of method overloads, you have default variables. Don't use mutable objects as your default value.

# bad
def fL(x=[])
  print x
# good
def fN(x=None)
  if (x is None):
    x = []
  print x


The difference between class and instance variables is very subtle when you first start out.

class Obj(object):
   thing = "class variable"
   def __init__(self):
      self.thing1 = "instance variable"
      print self.thing, self.thing1

Those are a few of the things that I had to get used to when I made the switch.

  • 1
    +1 good summary of some things, though I knew those from before
    – Anto
    Mar 28 '11 at 19:24

Well, Python doesn't have interfaces, does have metaclasses, and does allow duck typing. Python has list comprehensions, which are very powerful and don't exist in Java. Java has a rich type system with lots of data structures, and Python just has lists. So if you are leveraging what Python does have instead of trying to recreate what Java has in Python, you're probably writing Pythonic code.

But as far as OO code goes, there are certain style fundamentals that shouldn't change from language to language: you should always strive to write code that is Shy and DRY, whether you are writing in Applescript, Python, Java, or C++.


As @delnan pedantically points out, there are actually FIVE composite data types defined by Python at the Kernel level (list, dict, tuple, set, and frozenset, according to my copy of "Python in a Nutshell"). While this is true, it isn't actually relevant to the point I'm trying to make: Python builds on lists as the essential data structure. Yes, you CAN use a list as a stack, but you can use the exact same list as a queue. And then a stack again.

Java, on the other hand, has one kernel data structure (Array, according to "The Java Pocket Guide), but in general use, you can't get much done in Java without importing collections. Once you do that, you have access to a 'rich' (in which sense I mean immensely complex) type library with which to get the same functionality you had with Python's list.

Of course, both languages have classes and Java has interfaces, but while those are composite data types, they aren't really data structures in a textbook sense.

One difference being that you can't pop an item from a Java Queue, and you can't pass a Java Queue object somewhere that's expecting a Java Linked List. So perhaps by "rich" I actually mean "rigid".

So to explain what I mean by saying "Python just has lists", what I mean is that you can do pretty much everything you need to do in Python that you would do with Java Collections using Pythons List type. This single type does the work of a great many types in Java.

What does this mean for the Python programmer? It means that you can leverage the Python List type to write very pithy, direct code without the use of additional libraries--and pithiness (that is, the characteristic of conveying more value in fewer characters) is a core characteristic of "Pythonic" code.

  • I'm familiar with all but metaclasses, will look them up. Thank you :)
    – Anto
    Mar 28 '11 at 19:31
  • 7
    -1 until you can explain the following: (1) "Python just has lists" - Python has a wealth of data structures. It doesn't have three implementations of every single data structure ever conceived, but still about every one most people will ever need. (2) Calling Java's type system "rich" is a mockery of those really sophisticated type systems. For starters, look at Haskell (98 without any extensions).
    – user7043
    Mar 28 '11 at 19:58
  • I'm sorry, this just isn't true. Python has exactly two data structures: Lists and Dictionaries. Some Python LIBRARIES might extend these core structures, but that isn't the same as saying that the language has them.
    – philosodad
    Mar 28 '11 at 19:59
  • 5
    That's already twice as many as the answer names. The list doubles as stack. Sets and tuples are also built-in (how many data structures are built-in into Java?) There are also modules in the standard library for (min-)heaps, deques, immutable records and tightly-packed homogenous arrays (restricted to C types). And that's just from the top of my head. Yeah, most of those use lists/dicts internally (however, sets are not dicts with unused keys). But so are most collections in Java - in fact, in all languages. That's how it works.
    – user7043
    Mar 28 '11 at 20:05
  • 1
    Now I think I understand the point you tried to make (and removed my downvote - which I added in the first place because that part was plain wrong the way it was originally stated). I still think you need to consider at least two data structures (lists as almost-universal sequences and dicts as almost-universal mappings). And that's not to mention the various iterators and generators, which I use at least as (and propably even more) frequently as lists.
    – user7043
    Mar 29 '11 at 13:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.