Dict() constructor

Hi All,

I am currently at the study drills section of the “Dictionaries, Oh Lovely Dictionaries” chapter of the book.

While doing some studies on the dictionaries method on the Internet, I came across this two different statements on the object being returned by the dict() constructor (refer to the highlighted portion of pic 1 and 2 in circle attached in this thread).

Pic 1 highlighted portion statement: “The dict() doesn’t return any value (returns None)”
Pic 2 highlighted portion statement: “Return a new dictionary initialized from an optional positional argument and a possibly empty sets of keyword arguments.”

Based on my understanding, the dict() constructor should return a dictionary object as stated in Pic 2 and not “None” as stated in Pic 1 so that we could work on the dictionary object based on the methods available to it. Could someone kindly help to explain which of the statements is correct (the highlighted portion in pic 1 or 2) as the explanation from this two different sources is pretty confusing to me?


I can not find this excerpt in the Python3 book. For a second I thought you study Python2, But can’t find this excerpt in the "Dictionaries, Oh Lovely Dictionaries in Python2 book either.
So I guess your question is why does an empty dictionary return None?
Well, if you set the kwargs it will return those values.

Hey @JY93, so the dict() has to return a dict. I mean, that’s the entire point. I think that pic 1 is wrong. Do you know where you read that?

@zedshaw: Hey Zed, thanks for the clarification.

Here you go, the link to the statement that I have highlighted in pic 1 is as below:

[edited for anti-spam]

Yeah I don’t know what they’re talking about, so just ignore that it returns nothing. I mean, it’s a constructor so maybe they mean it doesn’t have a return, but it doesn’t give you a None either. No idea what they mean.

I looked at the page you linked. The output for the Example 1 didn’t make any sense to me. It looks like a copy of the code, not output!! The sentence you circled didn’t make any sense to me either.
Soooooo, time to start up my python interpreter in Terminal!!
Here’s what I found:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> empty = dict()
>>> print(empty)
>>> print('empty= ', empty)
empty=  {}
>>> print(type(empty))
<class 'dict'>
>>> dict()
>>> dict(one=1, two=2, three=3)
{'one': 1, 'two': 2, 'three': 3}

As you can see, dict() doesn’t return None. It constructs a dict which is empty.

Your pic 2 is from Python Docs: docs.python.org/3/library/stdtypes.html#mapping-types-dict

I’m still a noob who likes to browse news.ycombinator.com (even though I don’t understand most of what’s talked about), but I’ve never seen anyone anywhere complain that Python Docs are mistaken (Zed can correct me if I’m wrong).

You asked a good question! This is why I like browsing this forum. I’m always learning something from the questions asked.


Python’s docs are a really good description what the APIs do. About their only failing is they aren’t as good on how to actually use them to do things, but that is a universal common problem in software documentation.