listing all property variables of a class instance

Neil Cerutti horpner at yahoo.com
Mon Jun 25 13:09:25 EDT 2007


On 2007-06-25, André <andre.roberge at gmail.com> wrote:
> Suppose I define a class with a number of variables defined as
> properties.  Something like:
>
> class MyClass(object):
>
>     def __init__(self):
>         self.some_variable = 42
>         self._a = None
>         self._b = "pi"
>
>     def get_a(self):
>         return self._a
>     def set_a(self, value):
>         self._a = value
>
>     def get_b(self):
>         return self._b
>     def set_b(self, value):
>         self._b = value
>
>     a = property(get_a,  set_a, None, "a is a property")
>     b = property(get_b,  set_b, None, "b is a property")
>
> Is there a way to write a method that would list automatically
> all the variables defined as a property (say by printing their
> docstring and/ or their value), and only those variables?

This is off the cuff. There's likely a better way.

for k, v in MyClass.__dict__.iteritems():
  if isinstance(v, property):
    print k, v.__doc__

-- 
Neil Cerutti
22 members were present at the church meeting held at the home of Mrs. Marsha
Crutchfield last evening. Mrs. Crutchfield and Mrs. Rankin sang a duet, The
Lord Knows Why. --Church Bulletin Blooper



More information about the Python-list mailing list