Re: Checking if a variable is a dictionary

2008-03-10 Thread Steven D'Aprano
On Mon, 10 Mar 2008 14:29:48 +, 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 be

Re: Checking if a variable is a dictionary

2008-03-10 Thread Andrew Koenig
>> if type(a) is dict: >> print "a is a dictionnary!" > class MyDict(dict): > pass > a = MyDict() > type(a) is dict > => False isinstance(a, dict) => True 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 a

Re: Checking if a variable is a dictionary

2008-03-10 Thread Bruno Desthuilliers
Guillermo a écrit : > 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,

Re: Checking if a variable is a dictionary

2008-03-09 Thread Gabriel Genellina
On 9 mar, 11:23, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Sun, 09 Mar 2008 06:58:15 -0700, Guillermo wrote: > > 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 expec

Re: Checking if a variable is a dictionary

2008-03-09 Thread castironpi
On Mar 9, 9:23 am, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Sun, 09 Mar 2008 06:58:15 -0700, Guillermo wrote: > > 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 exp

Re: Checking if a variable is a dictionary

2008-03-09 Thread Steven D'Aprano
On Sun, 09 Mar 2008 06:58:15 -0700, Guillermo wrote: > 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 er

Re: Checking if a variable is a dictionary

2008-03-09 Thread Guillermo
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 cat

Re: Checking if a variable is a dictionary

2008-03-09 Thread Steven D'Aprano
On Sun, 09 Mar 2008 05:20:41 -0700, Guillermo wrote: >>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. > > Hm... I've s

Re: Checking if a variable is a dictionary

2008-03-09 Thread Guillermo
>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. Hm... I've searched about the implementation of protocols and now (I beli

Re: Checking if a variable is a dictionary

2008-03-06 Thread Neil Cerutti
On Thu, Mar 6, 2008 at 12:17 PM, Guillermo <[EMAIL PROTECTED]> wrote: > > 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 > >

Re: Checking if a variable is a dictionary

2008-03-06 Thread Guillermo
> 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 r

Re: Checking if a variable is a dictionary

2008-03-06 Thread Neil Cerutti
On Thu, Mar 6, 2008 at 8:06 AM, Guillermo <[EMAIL PROTECTED]> wrote: > 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':"

Re: Checking if a variable is a dictionary

2008-03-06 Thread Bruno Desthuilliers
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: pr

Re: Checking if a variable is a dictionary

2008-03-06 Thread Bruno Desthuilliers
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

Re: Checking if a variable is a dictionary

2008-03-06 Thread Martin Marcher
On Thu, Mar 6, 2008 at 2:06 PM, Guillermo <[EMAIL PROTECTED]> 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 > >

Re: Checking if a variable is a dictionary

2008-03-06 Thread Bjoern Schliessmann
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 proble

Re: Checking if a variable is a dictionary

2008-03-06 Thread Jeffrey Seifried
On Mar 6, 7:10 am, Guillermo <[EMAIL PROTECTED]> 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, >

Re: Checking if a variable is a dictionary

2008-03-06 Thread Guillermo
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 >s

Re: Checking if a variable is a dictionary

2008-03-06 Thread Bruno Desthuilliers
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 -- http://mail.python.org/mailman/listinfo/python-list

Re: Checking if a variable is a dictionary

2008-03-06 Thread Bruno Desthuilliers
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

Re: Checking if a variable is a dictionary

2008-03-06 Thread Gabriel Genellina
En Thu, 06 Mar 2008 10:10:47 -0200, Guillermo <[EMAIL PROTECTED]> 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" i

Re: Checking if a variable is a dictionary

2008-03-06 Thread Sam
Hello if type(a) is dict: print "a is a dictionnary!" ++ Sam -- http://mail.python.org/mailman/listinfo/python-list

Checking if a variable is a dictionary

2008-03-06 Thread Guillermo
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 -- http://mail.python.org/mailman/listinfo/python-list