RE: [PyCrust] Should ._getAttributeNames() be recursive?
Brought to you by:
pobrien
|
From: Patrick K. O'B. <po...@or...> - 2001-08-14 15:07:22
|
This implementation was far from perfect, and couldn't really be fixed. So I
have reversed my position and made ._getAttributeNames() responsible for
walking its own tree. The changes have been checked into cvs for both
PyCrust and PythonCard.
---
Patrick K. O'Brien
Orbtech
"I am, therefore I think."
-----Original Message-----
From: pyc...@li...
[mailto:pyc...@li...]On Behalf Of Patrick K.
O'Brien
Sent: Monday, August 13, 2001 10:41 AM
To: pycr
Subject: [PyCrust] Should ._getAttributeNames() be recursive?
PythonCard has implemented the previously discussed ._getAttributeNames()
method to expose magic attributes to introspection to extend command
autocompletion in the PyCrust shell. As discussed, this method was going to
walk up its inheritance tree recursively. When I added this method to the
PyCrust function that gets attributes, I added it in such a way that it
would be called recursively as well. (See implementation below.)
So the question is, who should be responsible for walking the tree. I see
two possible arguments:
1. From a pure design point-of-view, I would agree with the original plan
that said ._getAttributeNames() should walk the tree. That solves the
problem one time and in the right place.
2. From a Python point-of-view, I would say that dir() has been broken a
long time and most everyone already has code to walk the tree, just like I
did. Adding an extra call to object._getAttributeNames() is easy, so why not
just work the way dir() does, which is to not walk the tree.
The rebuttal to the second argument is that just because dir() is broken
doesn't mean we should get sloppy ourselves. And there has been some talk
(IIRC) of fixing dir() as part of the whole "type unification" effort. (Or
whatever it's called.) So dir() may actually get fixed.
Below is the current PyCrust code. If we decide that ._getAttributeNames()
should walk the tree I can change these PyCrust functions:
def getAttributeNames(object):
"""Return list of unique attributes, including inherited, for an
object."""
attributes = []
dict = {}
# Remove duplicates from the attribute list.
for item in getAllAttributeNames(object):
dict[item] = None
attributes += dict.keys()
attributes.sort()
return attributes
def getAllAttributeNames(object):
"""Return list of all attributes, including inherited, for an object.
Recursively walk through a class and all base classes.
"""
attributes = []
try:
# Get attributes available through the normal convention.
attributes += dir(object)
# Try to get any magic attributes.
try:
attributes += object._getAttributeNames()
except AttributeError:
pass
# For a class instance, get the attributes for the class.
if hasattr(object, '__class__'):
attributes += getAllAttributeNames(object.__class__)
# Also get attributes from any and all parent classes.
if hasattr(object, '__bases__'):
for base in object.__bases__:
attributes += getAllAttributeNames(base)
finally:
return attributes
---
Patrick K. O'Brien
Orbtech
"I am, therefore I think."
_______________________________________________
PyCrust-users mailing list
PyC...@li...
http://lists.sourceforge.net/lists/listinfo/pycrust-users
|