This:

>>> dict(one=1,two=2,three=3)
{'three': 3, 'two': 2, 'one': 1}

Is a shortcut for the longer:

>>> dict((('one',1),('two',2),('three',3)))
{'three': 3, 'two': 2, 'one': 1}

and given how this works:

>>> def function(**kwargs):
...  print kwargs
...  
>>> function(one=1,two=2,three=3)
{'three': 3, 'two': 2, 'one': 1}

One can guess how the shortcut is implemented.

-Mark

"wormwood_3" <[email protected]> wrote in message 
news:[email protected]...
        dict( [arg]) 
  Return a new dictionary initialized from an optional positional argument or 
from a set of keyword arguments. If no arguments are given, return a new empty 
dictionary. If the positional argument arg is a mapping object, return a 
dictionary mapping the same keys to the same values as does the mapping object.


  But why doesn't the optional positional argument arg in this case, not being 
a mapping type, get evaluated?: dict(thing=1)

  And even if it makes sense for it not to be evaluated, wouldn't it be better 
for dict() to complain that it didn't get a string or an int as it expects for 
a keyword argument? Maybe I am missing the use case, so far it just seems 
strange to force the keyword to a string.

  -Sam






------------------------------------------------------------------------------
  From: bob gailer <bgai...@g mail.com>
  To: wormwood_3 <[email protected]>
  Cc: Python Tutorlist <[email protected]>
  Sent: Wednesday, January 21, 2009 11:25:12 PM
  Subject: Re: [Tutor] dict() versus {}

  wormwood_3 wrote: 
    Hmm, looked through the latest docs, in the sections on dictionary types, 
don't see examples that point to this case well. Can you link to what you had 
in mind?


  2.1 Built-in Functions 
  ...
  dict( [mapping-or-sequence]) 
  ...
  these all return a dictionary equal to {"one": 2, "two": 3}: 
  ...
  dict(one=2, two=3) 





----------------------------------------------------------------------------
    From: bob gailer <[email protected]>
    To: wormwood_3 <[email protected]>
    Cc: Python Tutorlist <[email protected]>
    Sent: Wednesday, January 21, 2009 11:02:35 PM
    Subject: Re: [Tutor] dict() versus {}

    wormwood_3 wrote: 
      When creating a list of dictionaries through a loop, I ran into a strange 
issue. I'll let the code talk:

      >>> l = 'i am a special new list'.split()
      >>> t = []
      >>> for thing in l:
      ...     t.append({thing: 1})
      ... 
      >>> t
      [{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'new': 1}, {'list': 1}]

      This is what I expected. {} says to make a dictionary. Thing, not being 
quoted, is clearing a variable, which needs to be evaluated and used as the key.

      >>> t = []
      >>> for thing in l:
      ...     t.append(dict(thing=1))
      ... 
      >>> t
      [{'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, 
{'thing': 1}]

      This was what threw me. Why would the dict() function not evaluate thing? 
How can it take it as a literal string without quotes?
    I suggest you look dict up in the Python documentation. There it shows the 
result you got as an example. When in doubt read the manual.


    -- 
    Bob Gailer
    Chapel Hill NC
    919-636-4239
----------------------------------------------------------------------------
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor
  


  -- 
  Bob Gailer
  Chapel Hill NC
  919-636-4239


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


  _______________________________________________
  Tutor maillist  -  [email protected]
  http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to