472,951 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 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 1653
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
6
by: MP | last post by:
Hello I want to have a public property (and Brows able) in one control, I use this code: public System.Windows.Forms.Form recordForm get { return _recordForm;} set {_recordForm = value;}
5
by: Vicky via DotNetMonster.com | last post by:
Hi, I need help with "An object reference is required for the nonstatic field, method, or property 'dataReader.Class1.data'" Before I put folowing variable in class level, it works fine....
8
by: critter | last post by:
I am trying to get a value of a property of a class, but not an instance of the class. Using Reflection, I can find the class in my assembly, and I can find the property of my class, however, I...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
8
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
37
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
5
by: TS | last post by:
is it preferred to access member variables directly in code, on the page that declared them, versus going thru a property accessor? I would think that since theres no security concerns or anything...
6
by: =?Utf-8?B?UGhpbGw=?= | last post by:
I am making the transition from VB to C# and am stumped on a seemingly simple task. I have created a class and defined a property using set/get. Now from one of my forms I want to set the...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.