What happens if somebody passes a unicode string to your function? Or a class derived from dict? Or a class implementing a dict-like interface? Following code covers first two cases. If you are using Python 2.6 you might want to use collections.Mapping
instead of dict
as per the ABC PEP.
def value_list(x):
if isinstance(x, dict):
return list(set(x.values()))
elif isinstance(x, basestring):
return [x]
else:
return None
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…