Python is not Java

Thursday 2 December 2004This is 20 years old. Be careful.

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]
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]
My brain just exploded.
[gravatar]
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:
Comment text is Markdown.