A couple of OOPython questions

Alexy Khrabrov braver at pobox.com
Tue Sep 9 12:12:39 EDT 2003


Reading python books feels a bit like reading Perl books -- although
OO is there, some fundamental questions are not illuminated, and then
OO comes after scripting or text, as in the Cookbook!  E.g., a = b is
a deep copy.  How do you do shallow copy?  What are the equivalent of
copy constructors?

Here's another OO question for some real-life application, not that
puny regex fun (or was that perl? :)...  I've developed a CORBA server
for structural protein data, and made a simple python client for it
with omniORBpy.  My server wraps a huge DB2 database into a series of
accessors, each returning rows from the corresponding table.  Let's
say I just want to view the rows.  As I came to expect, the thing
(perl or python) will unravel and pretty-print any hierarchy of
dictionaries or lists.  [I can't help it, but perl books are shorter
since hash is a better term than a dictionary!  Just like with graphs,
where node saves you space over vertex.]

OK, now I go to my python prompt and I want to inspect my objects as
returned by the CORBA server.  One of them represents an atom site.  I
merrily type just its name to see the great unknown unwrap before my
eyes.  Here:

>>> a
<MMS.StructConf instance at 0x8cb1b44>

Hmm.  That's not what I hoped for.  Some fragments of code I saw run
through my mind and I type in,

>>> dir(a)
['_NP_RepositoryId', '__doc__', '__init__', '__module__', 'beg_auth',
'beg_label', 'conf_type', 'details', 'end_auth', 'end_label', 'id']

Good, we're getting somewhere.  I also googled out vars(a), which
seems to be the same as a.__dict__ (are they?...  why is __dict__ not
shown above then?  2.2...)

Printing out gazillion fields by hand for each class is silly, they
only wrap strings and numbers and other objects of that nature.  I am
ready to dive in a recursive descent.  No such luck -- whoever created
CORBA bindings for python didn't make those objects dictionary-like! 
I can't get members by name, although they _are_ in some __dict__,
useless apparently, until I supply __getitem__!

Is there an easy way to write something _once_, general enough, to
print out a hierarchy of nested objects which just don't have
__getitem__, although every field is either a string or a number or
another object?

Cheers, 
Alexy Khrabrov




More information about the Python-list mailing list