On Thu, Mar 6, 2008 at 8:06 AM, Guillermo
<[EMAIL PROTECTED]> wrote:
> I want to iterate recursively a dictionary whose elements might be
> strings or nested tuples or dictionaries and then convert values to a
> tagged format according to some rules.
>
> d = {'a':"i'm a", 'b':(1,2,3),'c':{'a':"i'm a",'x':"something",'y':
> ('a','b','c')}}
This could be solved with dynamic polymorphism instead of
introspection, which might simplify things depending on how your
dictionary is constructed.
class Value(object):
def to_tagged_format(self):
raise NotImplementedError
class StringValue(Value):
def to_tagged_format(self):
...
class Tuplevalue(Value):
def to_tagged_format(self):
...
class DictValue(Value):
def to_tagged_format(self):
...
for k, v in d.iteritems():
d[k] = v.to_tagged_format()
You can also get the dynamic polymorphism without invoking inheritance
by specifying a protocol that the values in your dict must implement,
instead. Protocols are plentiful in Python, perhaps more popular than
type hierarchies.
--
Neil Cerutti <[EMAIL PROTECTED]>
--
http://mail.python.org/mailman/listinfo/python-list