Traversing the properties of a Class

E

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.
 
N

Neil Cerutti

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
 
D

Daniel Nogradi

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

:)
 
E

EdG

Thanks.

Neil said:
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
 
E

EdG

Thanks.

Daniel said:
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

:)
 
B

Bruno Desthuilliers

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.
 
E

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.
 
B

Bruno Desthuilliers

EdG a écrit :
(top-post corrected)
> 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)))
 
E

EdG

That works perfectly thank you.

Bruno said:
EdG a écrit :
(top-post corrected)
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)))
 
S

Steven D'Aprano

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*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top