Python: multiple properties, one setter/getter -


consider following class definitions

class of2010(object):     def __init__(self):         self._a = 1         self._b = 2         self._c = 3      def set_a(self,value):         print('setting a...')         self._a = value     def set_b(self,value):         print('setting b...')         self._b = value     def set_c(self,value):         print('setting c...')         self._c = value     = property(fset=self.set_a)     b = property(fset=self.set_b)     c = property(fset=self.set_c) 

note set_[a|b|c]() same thing. there way define:

def set_magic(self,value):     print('setting <???>...')     self._??? = value 

once , use a,b,c follows

a = property(fset=self.set_magic) b = property(fset=self.set_magic) c = property(fset=self.set_magic) 

def attrsetter(attr):   def set_any(self, value):     setattr(self, attr, value)   return set_any  = property(fset=attrsetter('_a')) b = property(fset=attrsetter('_b')) c = property(fset=attrsetter('_c')) 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -