Using the result of type() in a boolean statement?

2008-11-12 Thread dpapathanasiou
If I define a dictionary where one or more of the values is also a dictionary, e.g.: my_dict={a:string, b:string, c:{x:0,y:1}, d:string} How can I use the output of type() so I can do one thing if the value is a string, and another if the value is a dictionary? i.e., I'd like to define a loop

Re: Using the result of type() in a boolean statement?

2008-11-12 Thread Terry Reedy
dpapathanasiou wrote: If I define a dictionary where one or more of the values is also a dictionary, e.g.: my_dict={a:string, b:string, c:{x:0,y:1}, d:string} How can I use the output of type() so I can do one thing if the value is a string, and another if the value is a dictionary? i.e., I'd

Re: Using the result of type() in a boolean statement?

2008-11-12 Thread Scott David Daniels
dpapathanasiou wrote: ... I'd like to define a loop like this, ... for key, value in my_dict.items(): if type{value) is type 'dict': # do the dictionary logic elif type(value) is type 'str': # do the string logic # etc You're searching for isinstance (or possibly issubclass)