win32com map attributes to methods

Gustavo Cordova gcordova at hebmex.com
Fri Jun 7 00:45:52 CEST 2002


Have you tried using property() in py2.2?

# YOU MUST DERIVE FROM "object"
class SimpleContainer(object):
   def __init__(self, default):
      self.__value = default

   def __get(self):
      print "<<< inside get() >>>"
      return self.__value

   def __set(self,value):
      print "<<< inside set() >>>"
      self.__value = value

   def __del(self):
      raise AttributeError("NO CAN DO, PAL!!!")

   value = property(__get, __set, __del, "docstring")

>>> sc = SimpleContainer(3)
>>> sc.__value
3
>>> sc.value
<<< inside get() >>>
3
>>> sc.value = 666
<<< inside set() >>>
>>> sc.__value
666
>>> sc.value
<<< inside get() >>>
666
>>> del sc.value
argh!!! (traceback)
NO CAN DO, PAL!
>>>

So, using property objects as class attributes,
you can gate access to internal data members,
or return calculated attributes, or create
read-only attributes.

Good luck!

-gustavo





More information about the Python-list mailing list