469,338 Members | 8,114 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

Traversing the properties of a Class

EdG
I'm using Python version 2.4 and I created a class with some properties
like:

def GetCallAmount(self):
return somedata

def GetCallCurrency(self):
return somemoredata

more....defs..etc.

CallAmount = property(GetCallAmount,None,None,None)
CallCurrency = property(GetCallCurrency, None, None, None)

more....properies..etc.

For debugging purposes, I would like to traverse the class listing out
all the properties.

Jan 18 '07 #1
9 1186
I'm using Python version 2.4 and I created a class with some properties
like:

def GetCallAmount(self):
return somedata

def GetCallCurrency(self):
return somemoredata

more....defs..etc.

CallAmount = property(GetCallAmount,None,None,None)
CallCurrency = property(GetCallCurrency, None, None, None)

more....properies..etc.

For debugging purposes, I would like to traverse the class listing out
all the properties.

for attr in dir( yourclass ):
if repr( yourclass.__dict__[ attr ] ).startswith( '<property' ):
print 'This looks like a property although can be something
else too: ' + attr

:)
Jan 18 '07 #2
On 2007-01-18, EdG <ed*******@yahoo.comwrote:
For debugging purposes, I would like to traverse the class
listing out all the properties.
This is the first thing that came to mind.

def show_properties(cls):
for attr in dir(cls):
if isinstance(getattr(cls, attr), property):
print attr

--
Neil Cerutti

--
Posted via a free Usenet account from http://www.teranews.com

Jan 18 '07 #3
EdG
Thanks.

Neil Cerutti wrote:
On 2007-01-18, EdG <ed*******@yahoo.comwrote:
For debugging purposes, I would like to traverse the class
listing out all the properties.

This is the first thing that came to mind.

def show_properties(cls):
for attr in dir(cls):
if isinstance(getattr(cls, attr), property):
print attr

--
Neil Cerutti

--
Posted via a free Usenet account from http://www.teranews.com
Jan 18 '07 #4
EdG
Thanks.

Daniel Nogradi wrote:
I'm using Python version 2.4 and I created a class with some properties
like:

def GetCallAmount(self):
return somedata

def GetCallCurrency(self):
return somemoredata

more....defs..etc.

CallAmount = property(GetCallAmount,None,None,None)
CallCurrency = property(GetCallCurrency, None, None, None)

more....properies..etc.

For debugging purposes, I would like to traverse the class listing out
all the properties.


for attr in dir( yourclass ):
if repr( yourclass.__dict__[ attr ] ).startswith( '<property' ):
print 'This looks like a property although can be something
else too: ' + attr

:)
Jan 18 '07 #5
EdG a écrit :
I'm using Python version 2.4 and I created a class with some properties
like:

def GetCallAmount(self):
return somedata
<mode="pep08">
The recommended naming convention is all_lower,ie:
def get_call_amount(self):
</mode>

And FWIW, there are idioms to avoid polluting the class namespace, like:

class Account(object):
@apply
def amount():
def fget(self):
return something
def fset(self, value):
do_something_with(value)
return property(**locals())
For debugging purposes, I would like to traverse the class listing out
all the properties.
cf Neil's answer.

Jan 18 '07 #6
EdG
This works great. I have one more question. Now that I have the name
of the property, how do I get it's value?

I want to print '%s = %s' % (attr,theattributesvalue)

Thanks.

Neil Cerutti wrote:
On 2007-01-18, EdG <ed*******@yahoo.comwrote:
For debugging purposes, I would like to traverse the class
listing out all the properties.

This is the first thing that came to mind.

def show_properties(cls):
for attr in dir(cls):
if isinstance(getattr(cls, attr), property):
print attr

--
Neil Cerutti

--
Posted via a free Usenet account from http://www.teranews.com
Jan 18 '07 #7
EdG a écrit :
(top-post corrected)
>
Neil Cerutti wrote:
>>On 2007-01-18, EdG <ed*******@yahoo.comwrote:
>>>For debugging purposes, I would like to traverse the class
listing out all the properties.

This is the first thing that came to mind.

def show_properties(cls):
for attr in dir(cls):
if isinstance(getattr(cls, attr), property):
print attr
This works great. I have one more question. Now that I have the name
of the property, how do I get it's value?

I want to print '%s = %s' % (attr,theattributesvalue)

Then you need to have the instance...

def list_properties(cls):
return [
name for name in dir(cls)
if isinstance(getattr(cls, name), property)
]

def print_properties(obj):
for name in list_properties(obj.__class__):
print "%s : %s" % (name, str(getattr(obj, name)))

Jan 18 '07 #8
EdG
That works perfectly thank you.

Bruno Desthuilliers wrote:
EdG a écrit :
(top-post corrected)

Neil Cerutti wrote:
>On 2007-01-18, EdG <ed*******@yahoo.comwrote:

For debugging purposes, I would like to traverse the class
listing out all the properties.

This is the first thing that came to mind.

def show_properties(cls):
for attr in dir(cls):
if isinstance(getattr(cls, attr), property):
print attr
This works great. I have one more question. Now that I have the name
of the property, how do I get it's value?
>
I want to print '%s = %s' % (attr,theattributesvalue)


Then you need to have the instance...

def list_properties(cls):
return [
name for name in dir(cls)
if isinstance(getattr(cls, name), property)
]

def print_properties(obj):
for name in list_properties(obj.__class__):
print "%s : %s" % (name, str(getattr(obj, name)))
Jan 18 '07 #9
On Thu, 18 Jan 2007 18:03:41 +0000, Neil Cerutti wrote:
On 2007-01-18, EdG <ed*******@yahoo.comwrote:
>For debugging purposes, I would like to traverse the class
listing out all the properties.

This is the first thing that came to mind.

def show_properties(cls):
for attr in dir(cls):
if isinstance(getattr(cls, attr), property):
print attr
Funny. The first thing that came to my mind was, "Thank you for sharing.
Did you have a question?"

*wink*

--
Steven.

Jan 19 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Plamen Valtchev | last post: by
1 post views Thread by alfred | last post: by
4 posts views Thread by nmosafi | last post: by
4 posts views Thread by plmanikandan | last post: by
reply views Thread by fig000 | last post: by
30 posts views Thread by asit | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.