a clean way to define dictionary
Alexander Schmolck
a.schmolck at gmx.net
Wed Jun 18 18:21:18 EDT 2003
Skip Montanaro <skip at pobox.com> writes:
> Alexander> Maybe I'm a bit blinkered, but right now I can't see how
>
> Alexander> dict(foo=1, bar='sean')
>
> Alexander> is so much better/more convinient than
>
> Alexander> {'foo':1, bar:'sean'}
>
> Alexander> that it justifies forcing people to learn a new redundant and
> Alexander> less general dictionary creation syntax that at least hinders
> Alexander> customizing dictionary instantiation like in
>
> I don't think it would normally be used that way. Instead, consider you
> have a preexisting dictionary and want a copy:
>
> >>> d1 = {'foo':1, 'bar':'sean'}
> >>> d2 = dict(**d1)
> >>> d2 == d1
> True
>
> That's a one-liner where the equivalent
>
> d2 = {}
> d2.update(d1)
>
> is a two-liner and likely slower.
Uhm,
d2 = d1.copy() ?
'as
More information about the Python-list
mailing list