listing all property variables of a class instance

  • Thread starter =?iso-8859-1?B?QW5kcuk=?=
  • Start date
?

=?iso-8859-1?B?QW5kcuk=?=

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é
 
N

Neil Cerutti

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__
 
J

Jay Loden

Neil said:
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__
 
M

Marc 'BlackJack' Rintsch

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
 
G

Gabriel Genellina

En Mon, 25 Jun 2007 15:10:25 -0300, Marc 'BlackJack' Rintsch
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.
 
?

=?iso-8859-1?B?QW5kcuk=?=

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é
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top