Properties for several keywords

Scott David Daniels Scott.Daniels at Acm.Org
Mon Jun 8 00:13:09 CEST 2009


Kless wrote:
> On 7 jun, 11:45, Kless <jonas.... at googlemail.com> wrote:
>> I've to write properties for several keywords with the same code, it
>> only changes the name of each property:
...
>> Is possible to simplify it?
> Please, is there any solution for this problem?

Read up on property.  It is the core of your answer.  That being said,
from your recent messages, it looks a lot like you are fighting the
language, rather than using it.  Pythonic code is clear about what
it is doing; avoid tricky automatic stuff.


     def mangled(name, doc_text=None):
         funny_name = '_' + name
         def getter(self):
             return getattr(self, funny_name)
         def setter(self, text):
             setattr(self, funny_name, self._mangle(text))
         return property(getter, setter, doc=doc_text)


     class MangledParts(object):
         blue = mangled('blue', "our side")
         red = mangled('red', "their side")
         white = mangled('white', "the poor civilians")

         def _mangle(self, text):
             print text
             return text.join('<>')


--Scott David Daniels
Scott.Daniels at Acm.Org




More information about the Python-list mailing list