472,328 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 1251
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: w.p. | last post by:
Hello! I want change default tab traversing in my app. But i don't know how to do it :( Belowe i include simple example - i want change default...
3
by: Plamen Valtchev | last post by:
This is my problem: From JavaScript I want to find the list of all defined/loaded JavaScript functions/objects/names within the current scope...
1
by: X | last post by:
I cannot figure this one out. I would like to traverse through and elements styles (not class) and get a list of all properties using javascript...
1
by: alfred | last post by:
Hi my question is on traversing a tree with DOM. how would I be able to traverse 2 trees at the same time. I have 2 XML documents, with similar...
4
by: nmosafi | last post by:
I have a datagrid that I am binding to a custom collection, and I am having problems with data binding in an ASP.NET datagrid (1.1 framework). ...
4
by: plmanikandan | last post by:
Hi, I am new to link list programming.I need to traverse from the end of link list.Is there any way to find the end of link list without...
0
by: fig000 | last post by:
Hi, I'm pretty new to the new dot.net. I've been able to quickly set up a grid view with page, etc. The gridview is connected to a drop down...
30
by: asit | last post by:
We kno that data can be pushed onto the stack or popped 4m it. Can stack be traversed ??
1
by: somcool | last post by:
I am facing an error while traversing a query in MS Access Details - When I click a button, a form which has the query opens up. There are certain...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.