Nick wrote:
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.

Cool.

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.


My understanding (confired via experimentation) is that with the 'traditional' way you can't orerride the getter or setter originally bound in by property call. The original methods will be used, not the ones in the subclass.

class Stuff(object):
    def getA(self):
        return 'stuff'

    a = property(getA)

class MoreStuff(object):
    def getA(self):
        return 'MORE stuff'


>>> thing = MoreStuff()
>>> print thing.a
stuff

But at any rate, these are basic python questions and I don't think python-dev is the correct forum for this discussion.

Thanks for giving me a couple of ideas to try out Nick.

Regards,
Jim


Reply via email to