Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > listing all property variables of a class instance

Reply
Thread Tools

listing all property variables of a class instance

 
 
=?iso-8859-1?B?QW5kcuk=?=
Guest
Posts: n/a
 
      06-25-2007
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é

 
Reply With Quote
 
 
 
 
Neil Cerutti
Guest
Posts: n/a
 
      06-25-2007
On 2007-06-25, André <> 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
 
Reply With Quote
 
 
 
 
Jay Loden
Guest
Posts: n/a
 
      06-25-2007

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__
 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      06-25-2007
In <mailman.35.1182792042.22759.python->, 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
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      06-25-2007
En Mon, 25 Jun 2007 15:10:25 -0300, Marc 'BlackJack' Rintsch
<> escribió:

> In <mailman.35.1182792042.22759.python->, 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

 
Reply With Quote
 
=?iso-8859-1?B?QW5kcuk=?=
Guest
Posts: n/a
 
      06-25-2007
On Jun 25, 2:09 pm, Neil Cerutti <horp...@yahoo.com> wrote:
> On 2007-06-25, André <andre.robe...@gmail.com> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is difference between Class variables and Instance variables? rahul8143@gmail.com Java 10 06-06-2011 06:43 AM
Class variables, instance variables, singleton; Ruby v. C++ Ralph Shnelvar Ruby 29 11-30-2009 07:43 PM
question about class variables and instance variables Eric D. Ruby 3 02-01-2006 07:57 PM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57