Useful code template for properties

  • Steve Lianoglou

    Steve Lianoglou - 2007-12-29

    Hi all,

    I don't know if there's some precedent for submitting templates that we think are useful, but here's one that I copied from TextMate's python bundle that's pretty nifty and comes in handy.

    It sets up for you the basic code to create a property in your class def. I have it bound to `prop` but  you can bind it to what you think is most appropriate:

    def ${prop}(): #@NoSelf
        doc = """${Docstring}"""
        def fget(self):
            return self._${prop}
        def fset(self, value):
            self._${prop} = value
        def fdel(self):
        return locals()
    ${prop} = property(**${prop}())${cursor}


    • Steve Lianoglou

      Steve Lianoglou - 2007-12-29

      Hmm, code formatting doesn't work. Let's try that again with the leading (...)'s being spaces:

      def ${prop}(): #@NoSelf
      ....doc = """${Docstring}"""
      ....def fget(self):
      ........return self._${prop}
      ....def fset(self, value):
      ........self._${prop} = value
      ....def fdel(self):
      ....return locals()
      ${prop} = property(**${prop}())${cursor}

    • Fabio Zadrozny

      Fabio Zadrozny - 2007-12-30

      Thanks for the template... I'll add it to the default templates.