Checking if a variable is a dictionary

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Thu Mar 6 09:47:39 EST 2008


Guillermo a écrit :
> Wow, I think I'm gonna like this forum. Thank you all for the prompt
> answers!

Welcome onboard !-)

>> What makes you say you "need" to know this ? Except for a couple corner
>> cases, you usually don't need to care about this. If you told us more
>> about the actual problem (instead of asking about what you think is the
>> solution), we might be of more help...
> 
> Good point.
> 
> 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.

If you're absolutely definitively 101% sure that you'll never have 
anything else in your dict - IOW : this dict is an internal part of your 
module, produced by the module and consumed by the module -, then it 
*might* be one of the corner cases where testing type (either directly 
or - preferably - via isinstance) is the right thing to do.

> d = {'a':"i'm a", 'b':(1,2,3),'c':{'a':"i'm a",'x':"something",'y':
> ('a','b','c')}}
> 
> I'm just designing the algorithm, but I think Python dictionaries can
> hold any kind of sequence?

Any Python object (which include classes, modules, functions etc) can be 
used as value.



More information about the Python-list mailing list