473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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 like this, but I'm not sure of the
syntax:

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
Nov 12 '08 #1
2 1364
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 like to define a loop like this, but I'm not sure of the
syntax:

for key, value in my_dict.items():
if type{value) is <type 'dict'>:
if type(v) is dict:
# do the dictionary logic
elif type(value) is <type 'str'>:
.... is str
# do the string logic
For built-in types without a built-in name, either import the types
module or just make one yourself with type().
>>func = type(lambda:1)
func
<class 'function'>
>>bif = type(abs)
bif
<class 'builtin_function_or_method'>

For userclass instances, use the userclass.

Terry Jan Reedy
Nov 12 '08 #2
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)
for key, value in my_dict.items():
if isinstance(value, dict):
# do the dictionary logic
elif isinstance(value, str): # use basestring for str & unicode
# do the string logic

Or, if you _must_ use type:
if issubclass(type(value), dict):
# do the dictionary logic
elif issubclass(type(value), str):
# do the string logic
--Scott David Daniels
Sc***********@Acm.Org
Nov 12 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
18
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
1
by: Thiero | last post by:
Hi I posted s thread but did have any reply, I am a new programmer and really wants someone to help me on how to use TreeMap for this code cos I want to it to be able to handle the options from 6...
4
by: Tugrul HELVACI | last post by:
Changing DisplayNames of my properties using PropertyGrid component, how ?? I'm using Delphi 2006 and I have a class defination like this: TPerson = class fPersonName : String;...
1
by: kommaraju | last post by:
iam a starter to db2 & jdbc.i have a servlet program which connects to ibm db2 using jdbc.when i run this using apache tomcat 4.1.34 , it is showing a error message of HTTP STATUS 500 my jdbc...
4
by: Marc | last post by:
Hi, I don't get it I cannot get this to work, can somebody give me a hint Table1 contains a field Id which is a GUID as primary key and DATA a string, I want to insert a new row but it does not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.