H
hetchkay
Hi,
For purposes I don't want to go into here, I have the following code:
def handleObj(obj):
if isinstance(obj, MyType):
return obj.handle()
elif isinstance(obj, (list, tuple, set)):
return obj.__class__(map (handleObj, obj))
elif isinstance(obj, dict):
return obj.__class__((handleObj(k), handleObj(v)) for k, v in
obj.items())
else:
return obj
This works fine except if obj is a namedtuple. A namedtuple object has
different constructor signature from tuple:
sample(a=1, b=2)
Is there any easy way of knowing that the obj is a namedtuple and not
a plain tuple [so that I could use obj.__class__(*map(handleObj, obj))
instead of obj.__class__(map(handleObj, obj)) ].
Thanks in advance for your help.
Krishnan
For purposes I don't want to go into here, I have the following code:
def handleObj(obj):
if isinstance(obj, MyType):
return obj.handle()
elif isinstance(obj, (list, tuple, set)):
return obj.__class__(map (handleObj, obj))
elif isinstance(obj, dict):
return obj.__class__((handleObj(k), handleObj(v)) for k, v in
obj.items())
else:
return obj
This works fine except if obj is a namedtuple. A namedtuple object has
different constructor signature from tuple:
Traceback (most recent call last):tuple([1,2]) (1,2)
collections.namedtuple("sample", "a, b")([1, 2])
sample(a=1, b=2)
Is there any easy way of knowing that the obj is a namedtuple and not
a plain tuple [so that I could use obj.__class__(*map(handleObj, obj))
instead of obj.__class__(map(handleObj, obj)) ].
Thanks in advance for your help.
Krishnan