Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Length of an Unsized Object

36 views
Skip to first unread message

MRAB

unread,
Dec 31, 2009, 12:09:05 PM12/31/09
to python-list
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.

What is the value of descr when it raises that exception? Try printing
descr (or repr(descr)) to find out.

BTW, len() always returns an int, so "len(descr) is not None" is always
true.

Dave Angel

unread,
Dec 31, 2009, 12:15:12 PM12/31/09
to Victor Subervi, python-list
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

0 new messages