![]() | Ned Batchelder : Blog | Code | Text | Site Python is not Java » Home : Blog : December 2004 |
Python is not JavaThursday 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:
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.
tagged:
python» 3 reactions | |
Comments
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
My brain just exploded.
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: