define
Turhan Ozen
txo at cs.nott.ac.uk
Mon May 12 12:44:54 EDT 2003
__getattr__ is getting into infinite loop. I just copied the code. Could
you please help me how to avoid this?
------------------------------------------------------------------------
Alex Martelli wrote:
It looks to me that a good solution is a "list with named items" class.
E.g., in Python 2.3 (warning, untested code):
class list_with_names(list):
def __init__(self, names):
list.__init__(self, len(names)*[None])
self.__names = {}
for i, name in enumerate(names):
self.__names[name] = i
def __getattr__(self, name):
try: return self.__names[name]
except KeyError: raise AttributeError
def __setattr__(self, name, value):
try: self[self.__names[name]] = value
except LookupError: object.__setattr__(self, name, value)
So instead of keeping your values in an 'array' (?), you'll do:
# e.g. a list of 5 items
x = list_with_names('aname another yetanother onemore andthistoo')
and then you can access and/or set e.g. x[1] equivalently to x.another.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20030512/01ec0cc7/attachment.html>
More information about the Python-list
mailing list