Python is not Java

Thursday 2 December 2004

Phillip Eby gives a long list of ways that Python is not Java. Talking about some Python code written by Java developers:

the sad thing is that these poor folks worked much, much harder than they needed to, in order to produce much more code than they needed to write, that then performs much more slowly than the equivalent idiomatic Python would.

He then describes in detail ways that Java idioms aren’t right for Python. One he mentioned I wasn’t familiar with: the property built-in function, new in Python 2.2.

Comments

[gravatar]
Matthew Scott 10:44 PM on 2 Dec 2004

property is a great function.

One interesting use is for proxying attributes in objects that act as facades for other objects.

For example:

class A(object):
def __init__(self):
self.b = B()
x = property(fget=lambda self: self.b.x)
y = 6

class B(object):
def __init__(self):
self.x = 5

>>> print A().x, A().y
5 6

[gravatar]
Jeff Atwood 12:10 AM on 3 Dec 2004

My brain just exploded.

[gravatar]
Alan Green 4:16 AM on 4 Dec 2004

I find myself using "property" to make public properties that are explicitly read-only. In Matthew's example above, B().x = 2 is allowed, but A().x = 2 would cause an exception.

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p><br><pre>.