I wonder if there is a reasonable easy way to allow for this code (with minor modifications) to work.
class Info(object):
@attr("Version")
def version(self):
return 3
info = Info()
assert info.version == 3
assert info["Version"] == 3
Ideally, the code would do some caching/memoising as well, e.g. employ lazy attributes, but I hope to figure that out myself.
Additional information:
The reason why I want provide two interfaces for accessing the same information is as follows.
I’d like to have a dict-like class which uses lazy keys. E.g. info["Version"] should call and cache another method and transparently return the result.
I don’t think that works with dicts alone, therefore I need to create new methods.
Methods alone won’t do either, because there are some attributes which are easier to define with pure dictionary syntax.
It probably is not the best idea anyway…
If the attribute name (
version) is always a lowercase version of the dict key ("Version"), then you could set it up this way:If you wish the dict key to be arbitrary, then its still possible, though more complicated:
I guess the larger question is, Is it a good interface? Why provide two syntaxes (with two different names) for the same thing?