[Tutor] dynamic property of a class ?

pan@uchicago.edu pan@uchicago.edu
Fri Jul 11 19:38:01 2003


I'm wondering if it's possible to make a class with dynamic
properties. For example, a normal class:

class car(object):

      def set_doors(self, val):
            self.__dict__['__doors']= val

      def get_doors(self):
            return self.__dict__['__doors']

      doors= property(get_doors, set_doors)

c = car()
c.doors = 4
print c.doors

So far so good.

Now, is it possible to make this:

c.color= 'black'
print c.color

and make 'color' a property (instead of an entry in the self.__dict__) 
at the runtime, even though it is not defined in the class ?

I'm imaging an approach like:

       self.__setproperty__(propName, getFunc, setFunc)

but is it even possible?

In the above example, "print type(t.doors)" doesn't give any info
regarding if 'doors' is a property or an entry in __dict__. Is it
possible to distinquish this at runtime ?


pan