473,395 Members | 1,401 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,395 software developers and data experts.

Checking if a variable is a dictionary


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
Mar 6 '08 #1
7 79240
Sam
Hello

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

++

Sam
Mar 6 '08 #2
En Thu, 06 Mar 2008 10:10:47 -0200, Guillermo
<gu**************@googlemail.comescribi�:
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

Mar 6 '08 #3
On Mar 6, 7:10 am, Guillermo <guillermo.lis...@googlemail.comwrote:
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'
Mar 6 '08 #4
You can also get the dynamic polymorphism without invoking inheritance
by specifying a protocol that the values in your dict must implement,
instead. Protocols are plentiful in Python, perhaps more popular than
type hierarchies.
I'm used to languages with stricter rules than Python. I've read a bit
about Python protocols, but where could I get some more info on
implementation details? Sounds very interesting.

Regards,

Guillermo
Mar 6 '08 #5
On Thu, Mar 6, 2008 at 12:17 PM, Guillermo
<gu**************@googlemail.comwrote:
You can also get the dynamic polymorphism without invoking inheritance
by specifying a protocol that the values in your dict must implement,
instead. Protocols are plentiful in Python, perhaps more popular than
type hierarchies.

I'm used to languages with stricter rules than Python. I've read a bit
about Python protocols, but where could I get some more info on
implementation details? Sounds very interesting.
A protocol is just an interface that an object agrees to implement. In
your case, you would state that every object stored in your special
dict must implement the to_tagged_value method with certain agreeable
semantics.

Python implements sequence types, mapping types, iterators, file
streams, and other things with protocols.

For example, iterators must support the "next" and and "__iter__" methods.

Using a protocol instead instead of a type hierarchy requires less
boring boilerplate, and I suppose in Python will usuallly be
preferable except when you want to inherit implementation as well as
interface. In that case, inheritance often saves boilerplate cide
rather than increases it.

It's also misnomered as duck-typing (clearly it should be nomed quack-typing).

--
Neil Cerutti <mr***************@gmail.com>
Mar 6 '08 #6

Mamma mia! My head just exploded. I've seen the light.

So you only need to ·want· to have a protocol? That's amazing... Far
beyond the claim that Python is easy. You define protocols in writing
basically! Even my grandma could have her own Python protocol.

Okay, so I think I know where's the catch now -- you must rely on the
fact that the protocol is implemented, there's no way to enforce it if
you're expecting a parrot-like object. You'd try to call the speak()
method and deal with the error if there's no such method?

Thanks a lot!

Guillermo
Mar 9 '08 #7
On Mon, 10 Mar 2008 14:29:48 +0000, Andrew Koenig wrote:
So the question you need to answer is whether you want to determine
whether an object is exactly of type dict, or whether it you are willing
to accept types derived from dict also.
Or other mappings that don't inherit from dict but behave just like
dicts, such as UserDict.
--
Steven
Mar 10 '08 #8

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

Similar topics

5
by: Tesla | last post by:
Hey guys, I want to PHP to do stuff only if a sertain form variable exists. How do i check to see if a variable exists or not? Something like If $http_post_vars exists, then do this. syntax...
2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
16
by: Ling Lee | last post by:
Hello. I'm trying to write a small program that lets you put in a number as an integer and then it tells you the textuel representation of the number. Like if your input is 42, it will say...
84
by: Andy Glew | last post by:
I am in search of any rigourous, scientific, academic or industrial studies comparing naming conventions in C++ or similar languages such as Ada: Specifically, are names formed with...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
10
by: Fredrik Tolf | last post by:
If I have a variable which points to a function, can I check if certain argument list matches what the function wants before or when calling it? Currently, I'm trying to catch a TypeError when...
8
by: Steph | last post by:
Hello, i tried to convert my code php into c#, but no hope... $foo = 'test'; $test = "hello world"; echo ${ $foo }; what is the equivalent in c# language ? thx
0
by: Gabriel Genellina | last post by:
En Fri, 18 Apr 2008 12:23:08 -0300, Shawn Milochik <Shawn@Milochik.comescribió: A dictionary with keys is perfectly reasonable. But a *list* of values has to be searched linearly for every...
1
by: GVDC | last post by:
Example server-side JavaScript Web script, Dictionary class //Dictionary class, hash array unlimited length configurable speed/efficiency // printf("<html><body>"); printf("<b>Creating...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.