Inspect Python Classes for instance data information

James Stroud jstroud at mbi.ucla.edu
Mon May 2 21:11:54 EDT 2005


Its not obvious what you are asking here. Try inheriting from object:

>>> class song(object):
...     def __init__(self, name, artist, album, published):
...         self.name = name
...         self.artist = artist
...         self.album = album
...         self.published = published
...     def build(self, name, artist, album, published):
...         self.name = name
...         self.artist = artist
...         self.album = album
...         self.published = published
...
>>> s = song("12:51", "The Strokes", "Room on Fire", 2005)
>>> dir(s)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
'__hash__', '__init__', '__module__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 
'album', 'artist', 'build', 'name', 'published']
>>> s.__class__
<class '__main__.song'>
>>> song
<class '__main__.song'>
>>> s.__dict__
{'album': 'Room on Fire', 'published': 2005, 'name': '12:51', 'artist': 'The 
Strokes'}
>>> dir(s.__class__)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
'__hash__', '__init__', '__module__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 
'build']
>>> s.build
<bound method song.build of <__main__.song object at 0x403ba10c>>


On Monday 02 May 2005 05:50 pm, so sayeth Tim Henderson:
> Hello
>
> I want to creat a program that can inspect a set of classes that i have
> made and spit out a savable version of these classes. To do this I need
> to be able to inspect each class and get all of its instance data as
> well as information about a particular meathod. This is what i want it
> to do:
>
>
> class song:
>     def __init__(self, name, artist, album, published):
>         self.name = name
>         self.artist = artist
>         self.album = album
>         self.published = published
>     def build(self, name, artist, album, published):
>         self.name = name
>         self.artist = artist
>         self.album = album
>         self.published = published
>
> #now what i want to do with this dummy class
> s = song("12:51", "The Strokes", "Room on Fire", 2005)
> print getTextVersion(s)
>
> <class>
>     <name>song</name>
>     <data>
>         <attribute>
>             <date_type>string</data_type>
>             <value>12:51</value>
>             <attribute_name>name</attribute_name>
>         </attribute>
>         <attribute>
>             <date_type>string</data_type>
>             <value>The Strokes</value>
>             <attribute_name>artist</attribute_name>
>         </attribute>
>         <attribute>
>             <date_type>string</data_type>
>             <value>Room on Fire</value>
>             <attribute_name>album</attribute_name>
>         </attribute>
>         <attribute>
>             <date_type>int</data_type>
>             <value>2005</value>
>             <attribute_name>published</attribute_name>
>         </attribute>
>     </data>
>     <build_parameter_order>name artist album
> published</build_parameter_order>
> </class>
>
>
> I have no idea how to do the inspection part of this little problem. I
> could just have each class define its own meathod to convert to this
> format, but that would be a lot of work when i could just make one
> class that solves the problem for all classes. Is there a way to do
> this?
>
> cheers
> ---
> Tim Henderson
> mail me: tim.tadh at gmail.com

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list