[Python-Dev] Re: [PEP 224] Attribute Docstrings
Alex Martelli
alex at magenta.com
Mon Aug 28 15:16:47 EDT 2000
"M.-A. Lemburg" <mal at lemburg.com> wrote in message
news:39AAA306.2CBD5383 at lemburg.com...
[snip]
> This will give you:
>
> A.__doc__x__ == " name of the database; override in subclasses ! "
> A.__doc__y__ == " run in auto-commit ? "
> D.__doc__x__ == """ name of the attached database; note that this must
support
> transactions
> """
> D.__doc__y__ == " run in auto-commit ? "
>
> There's no way you are going to achieve this using dictionaries.
But there must be... after all, if I ask for D.y, I get the same as A.y
by inheritance, don't I? Yet, it IS "achieved using dictionaries" --
plus, I guess, a tiny amount of "magic" (not particularly black) in
the lookup process (if y is not found in D.__dict__, we walk depth
first among the base classes, etc).
So, if it works for getattr (through the __dict__ and an appropriate
walk among bases), why shouldn't it work just as well for a
hypothetical getdoc...? Indeed, a "dictionary with inheritance from
other dictionaries" would seem to be a pretty natural building block
(conceptually identical to the lookup process for attributes, though
the latter may use its own shortcuts for speed); D.__docs__ would
just be one such.
E.g., if read-only, something like:
class compositeMapping:
def __init__(self, *bases, **ownvalues):
self.bases=bases
self.data=ownvalues
def __getattr__(self,key):
try:
return self.data[key]
except KeyError:
for base in bases:
try:
return base[key]
except KeyError:
pass
raise KeyError, key
Isn't this "achieved using dictionaries"? And couldn't it be just
what's wanted for SomeClass.__docs__...?
Alex
More information about the Python-list
mailing list