I wish I could add docstrings to vars.

M

Matthew Wilson

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
 
N

Neil Cerutti

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. ;)
 
M

Matthew Wilson

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)
 
N

Neil Cerutti

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.
 
M

Matimus

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top