Length of an Unsized Object

Dave Angel davea at ieee.org
Thu Dec 31 12:15:12 EST 2009


Victor Subervi wrote:
> Hi;
> I have this code:
>
>         sql = 'describe %s %s;' % (optionsStore, option)
>         print sql
>         cursor.execute(sql)
>         descr = cursor.fetchone()
>         if len(descr) is not None:
>
> Python complains:
>
> *TypeError*: len() of unsized object
>
> Please advise how to rewrite the last line so as to avoid the infamous
> try/except.
> TIA,
> beno
>
>   
What type of value do you expect 'descr' to get?  Have you tried 
printing type(descr) ?

I can only guess that the fetchone() method you're referring to is a 
method on sqlite3.Cursor.  If so, the docs I've got say:
   

     >>>Fetches the next row of a query result set, returning a single
    sequence, or None <constants.html#None> when no more data is available.

Of course None doesn't have a length, as it's not a collection, nor 
equivalent to one.  There's another thing wrong with your if-test.  
len() returns an integer, so it'll never be equal to None.  Presumably 
you're trying to check for an empty sequence.

So your if test would need to be something like:

     if  descr is not None and len(descr) > 0:

This could be simplified, but I'd rather be explicit.  But just for 
completeness, I believe the following would work as well:
     if descr:



DaveA





DaveA




More information about the Python-list mailing list