469,366 Members | 2,521 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

I wish I could add docstrings to vars.


I build a lot of elaborate dictionaries in my interpreter, and then I
forget exactly how they work. It would be really nice to be able to add
notes to the dictionary.

Is there some way to do this now?

Matt
--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Sep 12 '06 #1
4 874
On 2006-09-12, Matthew Wilson <ma**@tplus1.comwrote:
I build a lot of elaborate dictionaries in my interpreter, and
then I forget exactly how they work. It would be really nice
to be able to add notes to the dictionary.

Is there some way to do this now?
Writing a thin wrapper around the dictionary might be beneficial,
and would also furnish a place for the docstrings. Actually, the
wrapper would probably prevent you from needing the docstring
very often. ;)

--
Neil Cerutti
Eddie Robinson is about one word: winning and losing. --Eddie
Robinson's agent Paul Collier
Sep 12 '06 #2
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.

def vd(C):

"""

Return a subclass of class C that has a instance-level attribute _vardoc.

"""

class VDC(C):

def __init__(self, *args, **kwargs):

vardoc = kwargs.get('vardoc')
if vardoc:
assert isinstance(vardoc, str), "vardoc must be a
string!"
kwargs.pop('vardoc')
self._vardoc = vardoc

C.__init__(self, *args, **kwargs)

def __repr__(self):

if self._vardoc:
return self._vardoc + "\n" + C.__repr__(self)

else:
return C.__repr__(self)

return VDC
def test_vd():

i = vd(int)(6)
i._vardoc = "integer!"
assert isinstance(i, int)

d = vd(dict)(a=1, b=2, c=i, vardoc="dict!")
assert d['a'] == 1
assert d['c'] == 6
assert isinstance(d, dict)

--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Sep 12 '06 #3
On 2006-09-12, Matthew Wilson <ma**@tplus1.comwrote:
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
Sep 12 '06 #4
It seems that you are getting some complex answers with confusing
examples. So, here is hopefully a less confusing example:

class MyDict(dict):
"""MyDict doc-string!"""

#then to use it

d = MyDict()
d['something'] = whatever you want

This solution leaves it open to do whatever you want with the class,
but it's better than nothing.

Matthew Wilson wrote:
I build a lot of elaborate dictionaries in my interpreter, and then I
forget exactly how they work. It would be really nice to be able to add
notes to the dictionary.

Is there some way to do this now?

Matt
--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Sep 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sridhar R | last post: by
reply views Thread by Craig Ringer | last post: by
2 posts views Thread by Steven Bethard | last post: by
reply views Thread by Michael Muller | last post: by
12 posts views Thread by jelle | last post: by
4 posts views Thread by Lawrence D'Oliveiro | last post: by
reply views Thread by tjhnson | last post: by
6 posts views Thread by Steve Brown | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.