[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Checking if a variable is a dictionary

Guillermo

3/6/2008 12:11:00 PM


Hello,

This is my first post here. I'm getting my feet wet with Python and I
need to know how can I check whether a variable is of type dictionary.

Something like this:

if isdict(a) then print "a is a dictionary"

Regards,

Guillermo
22 Answers

samuel.progin

3/6/2008 12:15:00 PM

0

Hello

if type(a) is dict:
print "a is a dictionnary!"

++

Sam


Gabriel Genellina

3/6/2008 12:36:00 PM

0

En Thu, 06 Mar 2008 10:10:47 -0200, Guillermo
<guillermo.listas@googlemail.com> escribi�:

> This is my first post here. I'm getting my feet wet with Python and I
> need to know how can I check whether a variable is of type dictionary.
>
> Something like this:
>
> if isdict(a) then print "a is a dictionary"

if isinstance(a, dict): ...

But note that it's more common in Python to try to use it in the intended
way, and catch the possible errors.
Look for "duck typing" in this group, and the difference between "easier
to ask forgiveness
than permission" and "look before you leap".

--
Gabriel Genellina

Bruno Desthuilliers

3/6/2008 12:40:00 PM

0

Guillermo a écrit :
> Hello,
>
> This is my first post here. I'm getting my feet wet with Python and I
> need to know how can I check whether a variable is of type dictionary.

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...

> Something like this:
>
> if isdict(a) then print "a is a dictionary"

Should this work for subclasses of dicts ? Should this work for
non-subclasses of dict implementing the dict protocol ?

Bruno Desthuilliers

3/6/2008 12:41:00 PM

0

Sam a écrit :
> Hello
>
> if type(a) is dict:
> print "a is a dictionnary!"


class MyDict(dict):
pass

a = MyDict()

type(a) is dict
=> False

Guillermo

3/6/2008 1:07:00 PM

0


Wow, I think I'm gonna like this forum. Thank you all for the prompt
answers!

>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.

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?

Regards,

Guillermo

Jeffrey Seifried

3/6/2008 1:56:00 PM

0

On Mar 6, 7:10 am, Guillermo <guillermo.lis...@googlemail.com> wrote:
> Hello,
>
> This is my first post here. I'm getting my feet wet with Python and I
> need to know how can I check whether a variable is of type dictionary.
>
> Something like this:
>
> if isdict(a) then print "a is a dictionary"
>
> Regards,
>
> Guillermo

Or

if type(a)==type({}):
print 'a is a dictionary'

Bjoern Schliessmann

3/6/2008 2:24:00 PM

0

Guillermo wrote:

> I'm just designing the algorithm, but I think Python dictionaries
> can hold any kind of sequence?

(Watch out, dicts are no sequence types.)

I recommend relying duck typing as long as it's feasible. I. e. if
it can be subscripted like a dict, it is a dict. If this makes
problems, use the type({}) approach.

Regards,


Björn

--
BOFH excuse #56:

Electricians made popcorn in the power supply

Martin Marcher

3/6/2008 2:26:00 PM

0

On Thu, Mar 6, 2008 at 2:06 PM, Guillermo
<guillermo.listas@googlemail.com> wrote:
> >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...
>
> 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.

just do so, if it's not a dict you can always catch the exception,
then handle tuples (or the exception), then strings which should work
always (of course also error handling here according to your needs)

> I'm just designing the algorithm, but I think Python dictionaries can
> hold any kind of sequence?

python dicts can hold any kind of object (as a value). What you can't
do is have a list as the key, but that is easily circumvented by using
a tuple (or any other immutable type).


hth
martin

--
http://tumblr.ma...
https://twitter.com/Mar...
http://www.xing.com/profile/Mart...
http://www.linkedin.com/in/mar...

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.

Bruno Desthuilliers

3/6/2008 2:48:00 PM

0

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.

Bruno Desthuilliers

3/6/2008 2:53:00 PM

0

Jeffrey Seifried a écrit :
(snip)
> if type(a)==type({}):
> print 'a is a dictionary'

This instanciates a dict, call type() on it, and discard the dict -
which is useless since the dict type is a builtin. Also, when you want
to test identity, use an identity test.

if type(a) is dict:
print "blah blah blah"