Overriding properties

Nick Patavalis npat at efault.net
Sat Dec 11 20:06:00 EST 2004


Why does the following print "0 0" instead of "0 1"? What is the
canonical way to rewrite it in order to get what I obviously expect?

  class C(object):
      __val = 0
      def set_value(self, val):
          if val < 0 : self.__val = 0
          else : self.__val = val
      def get_value(self):
          return self.__val
      value = property(get_value, set_value)
  
  class CC(C):
      def set_value(self, val):
          if val < 0: self.__val = 1
          else : self.__val = val
  
  c = C()
  cc = CC()
  c.value = -1
  cc.value = -1
  print c.value, cc.value

Thanks in advance
/npat



More information about the Python-list mailing list