Re: [Python-Dev] performance of {} versus dict()

2012-11-14 Thread Mark Adam
On Wed, Nov 14, 2012 at 3:12 AM, Chris Withers  wrote:
> Hi All,
>
> A colleague pointed me at Doug's excellent article here:
> ...which made me a little sad, I suspect I'm not the only one who finds:
>
> a_dict = dict(
> x = 1,
> y = 2,
> z = 3,
> ...
> )
>
> ...easier to read than:
>
> a_dict = {
> 'x':1,
> 'y':2,
> 'z':3,
> ...
> }

Hey, it makes me a little sad that dict breaks convention by allowing
the use of unquoted characters (which everywhere else looks like
variable names) just for a silly typing optimization.

mark
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] performance of {} versus dict()

2012-11-14 Thread Mark Adam
On Wed, Nov 14, 2012 at 11:02 AM, Xavier Morel  wrote:
>
> On 2012-11-14, at 17:42 , Richard Oudkerk wrote:
>
>> On 14/11/2012 4:23pm, Serhiy Storchaka wrote:
>>> PEP 8 recommends:
>>>
>>> a_dict = dict(
>>> x=1,
>>> y=2,
>>> z=3,
>>> ...
>>> )
>>>
>>> and
>>>
>>> a_dict = {
>>> 'x': 1,
>>> 'y': 2,
>>> 'z': 3,
>>> ...
>>> }
>>
>> In which section?  I can't see such a recommendation.
>
> Whitespace in Expressions and Statements > Other Recommendations
>
> 3rd bullet:
>
> —
> Don't use spaces around the = sign when used to indicate a keyword argument 
> or a default parameter value.
>
> Yes:
>
> def complex(real, imag=0.0):
> return magic(r=real, i=imag)
>
> No:
>
> def complex(real, imag = 0.0):
> return magic(r = real, i = imag)

That's not a recommendation to use the **kwargs style.

mark
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] performance of {} versus dict()

2012-11-14 Thread Mark Adam
On Wed, Nov 14, 2012 at 11:00 AM, Brian Curtin  wrote:
> On Wed, Nov 14, 2012 at 10:12 AM, Mark Adam  wrote:
>> On Wed, Nov 14, 2012 at 3:12 AM, Chris Withers  
>> wrote:
>>> Hi All,
>>>
>>> A colleague pointed me at Doug's excellent article here:
>>> ...which made me a little sad, I suspect I'm not the only one who finds:
>>>
>>> a_dict = dict(
>>> x = 1,
>>> y = 2,
>>> z = 3,
>>> ...
>>> )
>>>
>>> ...easier to read than:
>>>
>>> a_dict = {
>>> 'x':1,
>>> 'y':2,
>>> 'z':3,
>>> ...
>>> }
>>
>> Hey, it makes me a little sad that dict breaks convention by allowing
>> the use of unquoted characters (which everywhere else looks like
>> variable names) just for a silly typing optimization.
>
> What convention and typing optimization is this? I hope you aren't
> suggesting it should be dict("x"=1) or dict("x":1)?

Try the canonical {'x':1}.  Only dict allows the special
initialization above.  Other collections require an iterable.  I'm guessing
**kwargs initialization was only used because it is so simple to
implement, but that's not necessarily a heuristic for good language design.

mark
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] performance of {} versus dict()

2012-11-14 Thread Mark Adam
On Wed, Nov 14, 2012 at 11:27 AM, R. David Murray  wrote:
> Maybe it's not good design, but I'll bet you that if it didn't do that,
> there would be lots of instances of this scattered around various
> codebases:
>
> def makedict(**kw):
> return kw

Now that's a good solution and probably solves the OP speed problem.

mark
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] performance of {} versus dict()

2012-11-14 Thread Mark Adam
On Wed, Nov 14, 2012 at 12:12 PM, Xavier Morel  wrote:
> On 2012-11-14, at 18:10 , Mark Adam wrote:
>>
>> Try the canonical {'x':1}.  Only dict allows the special
>> initialization above.  Other collections require an iterable.
>
> Other collections don't have a choice, because it would often be
> ambiguous. Dicts do not have that issue.

mkay

>>  I'm guessing
>> **kwargs initialization was only used because it is so simple to
>> implement, but that's not necessarily a heuristic for good language design.
>
> In this case it very much is, it permits easily merging two dicts in a
> single expression or cloning-with-replacement. It also mirrors the
> signature of dict.update which I think is a Good Thing.

Merging of two dicts is done with dict.update.   How do you do it on
initialization?  This doesn't make sense.

mark
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] performance of {} versus dict()

2012-11-14 Thread Mark Adam
On Wed, Nov 14, 2012 at 1:37 PM, Xavier Morel  wrote:
> On 2012-11-14, at 19:54 , Mark Adam wrote:
>>
>> Merging of two dicts is done with dict.update.
>
> No, dict.update merges one dict (or two) into a third one.

No.  I think you need to read the docs.

>> How do you do it on
>> initialization?  This doesn't make sense.
>
> dict(d1, **d2)

That's not valid syntax is it?

mark
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] performance of {} versus dict()

2012-11-14 Thread Mark Adam
On Wed, Nov 14, 2012 at 5:40 PM, Chris Angelico  wrote:
> On Thu, Nov 15, 2012 at 10:36 AM, Steven D'Aprano  wrote:
>> On 15/11/12 05:54, Mark Adam wrote:
>> Notice that I'm not merging one dict into another, but merging two dicts
>> into a third.
>
> Side point: Wouldn't it be quite logical to support dict addition?
>

Yes, but then you'd be in my old argument that dict should inherit from set.
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] performance of {} versus dict()

2012-11-14 Thread Mark Adam
On Wed, Nov 14, 2012 at 8:28 PM, Stephen J. Turnbull  wrote:
> Chris Angelico writes:
>
>  > >>> {"a":1}+{"b":2}
>
>  > It would make sense for this to result in {"a":1,"b":2}.
>
> The test is not "does this sometimes make sense?"  It's "does this
> ever result in nonsense, and if so, do we care?"
>
> Here, addition is usually commutative.  Should {'a':1}+{'a':2} be the
> same as, or different from, {'a':2}+{'a':1}, or should it be an error?

Easy:  dict should have a (user substitutable) collision function that
is called in these cases.   This would allow significant functionality
with practically no cost.  In addition, it could be implemented in
such a way as to offer significant speedups (when using dict.update
for example) over any possible hand-written substitutes (since it's
only run on key collisions and otherwise uses an underlying loop coded
in C).

mark
___
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com