Code to add docstrings to classes
Neil Cerutti
horpner at yahoo.com
Tue Sep 12 11:37:09 EDT 2006
On 2006-09-12, Matthew Wilson <matt at tplus1.com> wrote:
> On Tue 12 Sep 2006 10:06:27 AM EDT, Neil Cerutti wrote:
>> Writing a thin wrapper around the dictionary might be
>> beneficial, and would also furnish a place for the docstrings.
>
> I wrote a function that hopefully does just that. I'm not very
> savvy at doing this class-factory stuff, so any advice would be
> welcome.
I should have chosen my words more carefully. I meant to suggest
writing small interfaces for your dictionaries. This sort of
trick is often used in Scheme, where lists are used to represent
many different data types. Designing and writing a good interface
makes the code much easier to understand.
Here's a simple-minded example of a dictionary of identifiers for
use in an interpreter, which nevertheless demonstrates the
advantages of the technique.
class IdentError():
pass
idents = {}
def new_ident(id, initial_value=None):
"""
Create a new identifier, id, (possibly with an initial_value)
and add it to the table of identifiers. Raises IdentError if
the identifier already exists.
"""
if id in idents:
raise IdentError
else:
idents[id] = initial_value
def ident_value(id):
"""
Return the current value of id. Raises IdentError if the
identifier is undefined.
"""
if key not in idents:
raise IdentError
else:
return idents[id]
Then the rest of the code uses this tiny interface.
--
Neil Cerutti
If only faces could talk. --Pat Summerall
More information about the Python-list
mailing list