Traversing the properties of a Class

EdG edquichan at yahoo.com
Thu Jan 18 16:49:38 EST 2007


That works perfectly thank you.

Bruno Desthuilliers wrote:
> EdG a écrit :
> (top-post corrected)
> >
> > Neil Cerutti wrote:
> >
> >>On 2007-01-18, EdG <edquichan at yahoo.com> wrote:
> >>
> >>>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)))




More information about the Python-list mailing list