Python Babushka?

Branimir Petrovic BranimirPetrovic at yahoo.com
Tue Dec 17 20:38:24 EST 2002


#	FileName	testDirBuiltin.py
######################################################################
"""
Would a kind soul please help in shedding some light on a confusion
or two of mine:

- How to dynamically build a list of ALL attributes belonging to
arbitrary Python object (see item #1), regardless of attribute's
specialty? 

Reason for asking this is - built in dir function seems not to be
able to show all object attributes (yes I know this is normal
behavior with old style classes, but even with new style classes
- dir(obj) simply does not show all there is to see!?).

- Now on Babushka phenomenon: What is the true meaning (if any) of 
seemingly endless 'depth' of let's say - im_class attribute?

Stumbled across this while looking at a piece of code through Komodo's
debugger, confirmed it is so using command line (pls see item #2).

Does this falls into "now that you asked le'mme tell ya..." kind of 
thing - or seemingly infinite depth implies never ending chain of 
"chicken lays egg, that becomes new chicken that lays egg, etc." 
Or it means something entirely different?


Tnx,

 Branimir

P.S. Can you tell this post comes from Python newcomer?
"""

class Foo:
	def myMeth1(self): pass

class Bar(object):
	def myMeth2(self): pass

f = Foo()
b = Bar()

# Item of confusion #1.
print
print "List all 'old style' class instance attributes (as seen by
dir()):"
print '\tdir(f) -> %s' % (dir(f),)
print "List all attributes belonging to 'invisible' __class__
attribute:"
print '\tdir(f.__class__)  -> %s' % (dir(f.__class__),)
print
print "List all 'new style' class instance attributes:"
print '\tdir(b) -> %s' % (dir(b),)
print "List all attributes belonging to __class__ attribute:"
print '\tdir(b.__class__)  -> %s' % (dir(b.__class__),)
print
print "Request one 'invisible' attribute, for instance the __name__:"
print '\tb.__class__.__name__ -> %s' % (b.__class__.__name__,)
print '\tf.__class__.__name__ -> %s' % (f.__class__.__name__,)
print
print "Built in dir() does not list all object's attributes."
print "Am I missing some more attributes except __name__, who knows?" 
print "New style classes or not, how to reliably build a list of ALL"
print "(where ALL means - ABSOLUTELY ALL) object attributes???"
print

# Item of confusion #2.
print
print "Python implementation of Babushka"
print "        OR"
print "(What actually am I looking at?)"
print
print "Method object's __class__ attribute whose method object's class
attribute is..."
print
print '\ttype(b.myMeth2) -> %s' % (type(b.myMeth2),)
print '\tdir(b.myMeth2)  -> %s' % (dir(b.myMeth2),)
print
print '\ttype(b.myMeth2.im_class) -> %s' % (type(b.myMeth2.im_class),)
print '\tdir(b.myMeth2.im_class)  -> %s' % (dir(b.myMeth2.im_class),)
print
print '\ttype(b.myMeth2.im_class.myMeth2) -> %s' %
(type(b.myMeth2.im_class.myMeth2),)
print '\tdir(b.myMeth2.im_class.myMeth2)  -> %s' %
(dir(b.myMeth2.im_class.myMeth2),)
print
print '\ttype(b.myMeth2.im_class.myMeth2.im_class) -> %s' %
(type(b.myMeth2.im_class.myMeth2.im_class),)
print '\tdir(b.myMeth2.im_class.myMeth2.im_class)  -> %s' %
(dir(b.myMeth2.im_class.myMeth2.im_class),)
print
print "...whose method object's __class__ attribute... - until EOT
(End Ob Time)?"
print



More information about the Python-list mailing list