<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'><br><br><div>> From: steve+comp.lang.python@pearwood.info<br>> Subject: Re: Beginner question<br>> Date: Tue, 4 Jun 2013 12:25:27 +0000<br>> To: python-list@python.org<br>> <br>> On Tue, 04 Jun 2013 14:53:29 +0300, Carlos Nepomuceno wrote:<br>> <br>> > That's exactly the same!<br>> >>>>dict(**{a:0,b:1})=={a:0,b:1}<br>> > True<br>> <br>> <br>> Of course it is. Isn't that what you wanted?<br><br>Indeed! But that form isn't economically viable as you noted.<br>> <br>> It's also a waste of time, because you create a dict literal using {}, <br>> then unpack it into keyword arguments, then call dict() to create a new <br>> dict with the same content. Rather like doing this:<br>> <br>> n = int(str(42))<br>> <br>> only even more expensive.<br>> <br>> <br>> > Are there any benefits from using dict() instead of {}?<br>> <br>> Of course there are. {} can be used for creating dict literals, which <br>> means you are limited to key/values that you explicitly include. dict(), <br>> on the other hand, has a rich set of constructor APIs:<br>> <br>> py> help(dict)<br>> <br>> Help on class dict in module builtins:<br>> <br>> class dict(object)<br>> | dict() -> new empty dictionary<br>> | dict(mapping) -> new dictionary initialized from a mapping object's<br>> | (key, value) pairs<br>> | dict(iterable) -> new dictionary initialized as if via:<br>> | d = {}<br>> | for k, v in iterable:<br>> | d[k] = v<br>> | dict(**kwargs) -> new dictionary initialized with the name=value pairs<br>> | in the keyword argument list. For example: dict(one=1, two=2)<br>> <br>> <br>> py> dict(zip('abcd', range(4)), x=23, y=42, z=999)<br>> {'a': 0, 'c': 2, 'b': 1, 'd': 3, 'y': 42, 'x': 23, 'z': 999}<br><br>Awesome! Now I can do it just like that:<br><br>>>> dict([(chr(ord('a')+x),x) for x in range(2)])<br>{'a': 0, 'b': 1}<br><br>Thanks a lot! ;)<br><br></div> </div></body>
</html>