Iterate thru object member names

Greg Jorgensen gregj at pobox.com
Sun Jan 28 19:44:28 EST 2001


In article <9526tr$iac at dispatch.concentric.net>,
  Phlip <phlip_cpp at my-deja.com> wrote:
> Pythons:
>
> In Javascript you can iterate thru an object's members and print out
all
> their names. What's the equivalent in Python?

----
for a in dir(object):
    print a
----

dir() works on any Python object, including classes and functions.
Depending on what you are doing, you may also want to look at the
__dict__ attribute of your class:

  myclass.__dict__

The types module may also be of interest if you need to determine the
type of an attribute (function, string, etc.).

--
Greg Jorgensen
Portland, Oregon, USA
gregj at pobox.com


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list