pythonic use of properties?

Michele Simionato michele.simionato at gmail.com
Fri Apr 15 00:37:07 EDT 2005


I don't use properties that much, but when I use them
I put them *outside* the class, in a property factory.
Here is an example I have prepared for my course at
PyUK, using a property to crypt a password attribute:

 class User(object):
     def __init__(self, username, password):
         self.username, self.password = username, password


 def cryptedAttribute(seed="x"):
     def get(self):
         return getattr(self, "_pw", None)
     def set(self, value):
         self._pw = crypt(value, seed)
     return property(get, set)


 User.pw = cryptedAttribute()

I feel that:

1) separation of concerns is of the utmost importance;
2) classes should be kept as short as possible.

Notice that in this design getters and setters are
really hidden from the user, which may be or may be
not what you want.

             Michele Simionato




More information about the Python-list mailing list