Jim Gallacher wrote:
I've only started using properites and I hadn't thought of that. If we
were to use such a feature is that the sort detail that would be
included in the user documentation, or would the coder just be expected
to read the source?
You CAN read the source if you need implementation details, especially if
you're going to override the default behavior. However, in most cases I
think you're going to extend behavior, so you will eventually call the
superclass' getter/setter. A real problem with Python is that there's no
standard convention for writing property getters and setters. I myself use
an idiom like this:
def variable():
def fget(self): return None
def fset(self, value): pass
def fdel(self): pass
doc = """The documentation"""
return locals()
variable = property(**variable())
I like this idiom, because it's really easy to spot in the code, and it
keeps everything together for the property. This forces you to completely
override the property in a subclass, but you don't have to know the
implementation details of the superclass to do that.
Another way might be (minimally and traditionally):
def variable_getter(self): return None
def variable_setter(self): pass
variable = property(variable_getter, variable_setter)
which then allows you to override variable_getter or variable_setter in your
subclass without overriding the property itself, but now you've got to read
the code. Or else completely override the property, but you've got a choice.
Nick