473,320 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1661
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.