2009/5/27 PJ Fitzpatrick <[email protected]>:
> Hi,
> If i have a class name as a string how can i get a list of the field names
> for the class.
> tks,
> PJ

You can get the class by

>>> class A(object):
              pass
>>> locals()['A']
<class __main__.A at 0xf0cb90>

and you can get the class dictionary (methods and class attributes) like

>>> locals()['A'].__dict__.keys()
['__dict__', '__module__', '__weakref__', '__doc__']
>>>

but since python is a dynamic language it's only possible to know what
attributes are present by looking at instances. eg.

>>> a = A()
>>> a.__dict__.keys()
[]
>>> a.foo = 1
>>> a.__dict__.keys()
['foo']
>>> a.foo
1

HTH

-- 
steev
http://www.daikaiju.org.uk/~steve/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Python Ireland" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.ie/group/pythonireland?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to