471,356 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,356 software developers and data experts.

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é

Jun 25 '07 #1
5 1611
On 2007-06-25, André <an***********@gmail.comwrote:
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
Jun 25 '07 #2

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__
Jun 25 '07 #3
In <ma*************************************@python.or g>, 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
Jun 25 '07 #4
En Mon, 25 Jun 2007 15:10:25 -0300, Marc 'BlackJack' Rintsch
<bj****@gmx.netescribió:
In <ma*************************************@python.or g>, 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

Jun 25 '07 #5
On Jun 25, 2:09 pm, Neil Cerutti <horp...@yahoo.comwrote:
On 2007-06-25, André <andre.robe...@gmail.comwrote:
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

Jun 25 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by MP | last post: by
8 posts views Thread by critter | last post: by
8 posts views Thread by Lüpher Cypher | last post: by
6 posts views Thread by =?Utf-8?B?UGhpbGw=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.