Useful code template for properties

2007-12-29
2013-03-15
  • 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):
            del(self._${prop})
        return locals()
    ${prop} = property(**${prop}())${cursor}

    Enjoy,
    -steve

     
    • 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):
      ........del(self._${prop})
      ....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.

      Cheers,

      Fabio