Home

Jeff McNeil a écrit :
(top-post corrected)
> On 8/13/07, Astan Chee <stanc@al.com.au> wrote:
>> Hi,
>> I have a variable, I want to check if it is a dictionary or a string.
>> Is there any better way to do this than I've done. How I did it is by
>> doing a .items() and catching a AttributeError that it raises if its not
>> a dictionary.
>> How do i properly do it?
>> Thanks

> You could use 'isinstance' to determine whether or not your object is
> an instance of a particular class.
>
>>>> isinstance({}, dict)
> True
>>>> isinstance("", basestring)
> True
>
> Note the use of 'basestring', which will catch unicode as well.
>
The problem is that it will prevent passing some perfectly valid objects
(in this case, mostly dict-like objects, and there are quite a lot of
dict-like objects in existing Python code).

previous
next

Re: wxpython with python 2.5
Re: Is this a STLport bug
Re: Coming from Perl
Re: Combinatorial of elements in Python?
Re: Regular Expression Grouping
Fundacja Sloneczko
Nasze Dzieci
Akogo
Dzieci Niczyje
Rodzic Po Ludzku