I just found an interesting pattern while writing a class where I wanted to expose an attribute publicly, but in a read-only manner.
Here's the skeleton version of the code:
from operator import attrgetter
class Connection(object):
def __init__(self, uri):
self._uri = uri
uri = property(attrgetter('_uri'))
Ever since discovering operator, in particular attrgetter, I've been finding more useful places to use it. It makes me feel naive now to think that I would once write the above like this:
class Connection(object):
def __init__(self, uri):
self._uri = uri
@property
def uri(self):
return self._uri
For me, the first example is more meaningful at a glance.
{ 5 comments… read them below or add one }
Beautiful! :)
Very neat! I’ve always done:
uri = property(lambda x: x._url)
which though terse isn’t as clear as attrgetter!
Why not simply use property(lambda self: self._uri)?
Just for fun, how about this:
uri = property(lambda self: self._uri)
@Frank, @Wyatt: Mostly because I’ve been trained, from the people and code I’ve worked with, to avoid lambdas 99% of the time, giving preference to named function definitions. So, what I used to use was:
@property def abc(self): return self._abcUsing attrgetter makes it easy to turn that into a one-liner, without having to use lambda :) Plus, as Alexander points out, the name “attrgetter” certainly tells you pretty directly what’s going on.