[ python-Bugs-1276587 ] dict('') doesn't raise a value error

SourceForge.net noreply at sourceforge.net
Tue Aug 30 16:39:33 CEST 2005


Bugs item #1276587, was opened at 2005-08-30 08:52
Message generated for change (Comment added) made by tim_one
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1276587&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Mike Foord (mjfoord)
Assigned to: Nobody/Anonymous (nobody)
Summary: dict('') doesn't raise a value error

Initial Comment:
The dict function will theoretically accept any sequence 
or bounded iterable that yields (key, value) tuples.

A side effect is that dict('') is valid - producing an emtpy 
dictionary.

dict(x) where x is *any* string other than '' fails with a 
ValueError. I suggest that dict('') ought to produce a 
ValueError to as a string is *never* a valid input to the 
dict function.

The current situation allows obscure errors to pass 
unnoticed.

----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2005-08-30 10:39

Message:
Logged In: YES 
user_id=31435

It's not theoretical:  it's a fact that dict() accepts any iterable 
producing iterables each producing 2 objects (the latter don't 
have to be tuples; a (key, value) tuple is just one kind of 
iterable producing 2 objects; e.g., dict(["ab"]) == {'a': 'b'}).

An empty str meets the input requirements, so there's no 
way to stop this without special-case type-sniffing.  That's 
very unattractive, in part because it's impossible to guess 
which kinds of empty iterables would necessarily lead to an 
exception when passed to dict() had they not been empty.  
For example, passing an empty array.array (of any flavor) to 
dict() also constructs an empty dict.  The universe of iterable 
objects is vast.

Keeping it uniform and easy to explain (an empty iterable 
produces an empty dict) seems better to me than adding a 
maze of special cases that's bound to change over time 
("except for an empty str" ... "oh, or an empty 
unicode" ... "oh, or an empty array.array" ... "oh, or an empty 
file" ... "oh, oops, guess not, cuz a file with two lines works 
fine" ... etc).

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1276587&group_id=5470


More information about the Python-bugs-list mailing list