listing all property variables of a class instance
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? André -- http://mail.python.org/mailman/listinfo/python-list
Re: listing all property variables of a class instance
On 2007-06-25, André [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: listing all property variables of a class instance
Neil Cerutti wrote: 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__ The only way I could get this to work was to change the way the properties were defined/initalized: #!/usr/bin/python class MyClass(object): def __init__(self): self.some_variable = 42 self._a = None self._b = pi self.a = property(self.get_a, self.set_a, None, a is a property) self.b = property(self.get_b, self.set_b, None, b is a property) 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 test = MyClass() for k,v in test.__dict__.iteritems(): if isinstance(v, property): print k, v.__doc__ -- http://mail.python.org/mailman/listinfo/python-list
Re: listing all property variables of a class instance
In [EMAIL PROTECTED], Jay Loden wrote: Neil Cerutti wrote: 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__ The only way I could get this to work was to change the way the properties were defined/initalized: That's because you iterate over the instance's `__dict__` and not over the *class* `__dict__` like Neil does. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: listing all property variables of a class instance
En Mon, 25 Jun 2007 15:10:25 -0300, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] escribió: In [EMAIL PROTECTED], Jay Loden wrote: Neil Cerutti wrote: 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__ The only way I could get this to work was to change the way the properties were defined/initalized: That's because you iterate over the instance's `__dict__` and not over the *class* `__dict__` like Neil does. I would iterate over dir(MyClass) instead - only because I prefer to hide such implementation details. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: listing all property variables of a class instance
On Jun 25, 2:09 pm, Neil Cerutti [EMAIL PROTECTED] wrote: On 2007-06-25, André [EMAIL PROTECTED] 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__ Thank you, this solved my problem nicely. André -- 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 -- http://mail.python.org/mailman/listinfo/python-list