473,405 Members | 2,187 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,405 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 1312
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 tab order: radiobutton "mode11" -> radiobutton...
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 (html page in a browser). the page could contain...
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 property names and css property names along with the...
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 nodes. I would like to traverse 1 xml document,...
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). As a simple example, imagine a class called...
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 traversing from start(i.e traversing from first to find the...
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 which changes the gridview's contents based on the...
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 fields which are in the form of combo box in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.