Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
performance of {} versus dict()
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 66 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chris Withers  
View profile  
 More options Nov 14 2012, 4:13 am
From: Chris Withers <ch...@simplistix.co.uk>
Date: Wed, 14 Nov 2012 09:12:05 +0000
Local: Wed, Nov 14 2012 4:12 am
Subject: [Python-Dev] performance of {} versus dict()
Hi All,

A colleague pointed me at Doug's excellent article here:

http://www.doughellmann.com/articles/misc/dict-performance/index.html

...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,
     ...
     }

What can we do to speed up the former case?

Here's comparison for different versions of CPython:

$ python2.5 -m timeit -n 1000000 -r 5 -v 'dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7)'
raw times: 2.96 2.49 2.47 2.42 2.42
1000000 loops, best of 5: 2.42 usec per loop
$ python2.5 -m timeit -n 1000000 -r 5 -v
"{'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7}"
raw times: 1.69 1.71 1.68 1.68 1.68
1000000 loops, best of 5: 1.68 usec per loop

$ python2.6 -m timeit -n 1000000 -r 5 -v 'dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7)'
raw times: 2.41 2.41 2.42 2.44 2.41
1000000 loops, best of 5: 2.41 usec per loop
$ python2.6 -m timeit -n 1000000 -r 5 -v
"{'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7}"
raw times: 1.51 1.51 1.52 1.51 1.51
1000000 loops, best of 5: 1.51 usec per loop

$ python2.7 -m timeit -n 1000000 -r 5 -v 'dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7)'
raw times: 2.32 2.31 2.31 2.32 2.31
1000000 loops, best of 5: 2.31 usec per loop
$ python2.7 -m timeit -n 1000000 -r 5 -v
"{'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7}"
raw times: 1.49 1.49 1.77 1.76 1.55
1000000 loops, best of 5: 1.49 usec per loop

So, not the 6 times headline figure that Doug quotes, but certainly a
difference. Can someone with Python 3 handy compare there too?

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ulrich Eckhardt  
View profile  
 More options Nov 14 2012, 4:32 am
From: Ulrich Eckhardt <ulrich.eckha...@dominolaser.com>
Date: Wed, 14 Nov 2012 10:30:27 +0100
Local: Wed, Nov 14 2012 4:30 am
Subject: Re: [Python-Dev] performance of {} versus dict()
Am 14.11.2012 10:12, schrieb Chris Withers:

> Can someone with Python 3 handy compare there too?

C:\Python27\python -m timeit -n 1000000 -r 5 -v
    "dict(a=1, b=2, c=3, d=4, e=5, f=6, g=7)"
raw times: 0.918 0.924 0.922 0.928 0.926
1000000 loops, best of 5: 0.918 usec per loop

C:\Python27\python -m timeit -n 1000000 -r 5 -v
    "{'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7}"
raw times: 0.48 0.49 0.482 0.496 0.497
1000000 loops, best of 5: 0.48 usec per loop

C:\Python32\python -m timeit -n 1000000 -r 5 -v
    "dict(a=1, b=2, c=3, d=4, e=5, f=6, g=7)"
raw times: 0.898 0.891 0.892 0.899 0.891
1000000 loops, best of 5: 0.891 usec per loop

C:\Python32\python -m timeit -n 1000000 -r 5 -v
    "{'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7}"
raw times: 0.444 0.463 0.461 0.464 0.461
1000000 loops, best of 5: 0.444 usec per loop

C:\Python32_64\python -m timeit -n 1000000 -r 5 -v
    "dict(a=1, b=2, c=3, d=4, e=5, f=6, g=7)"
raw times: 0.908 0.923 0.927 0.912 0.923
1000000 loops, best of 5: 0.908 usec per loop

C:\Python32_64\python -m timeit -n 1000000 -r 5 -v
    "{'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7}"
raw times: 0.484 0.446 0.501 0.45 0.442
1000000 loops, best of 5: 0.442 usec per loop

C:\Python33_64\python -m timeit -n 1000000 -r 5 -v
    "dict(a=1, b=2, c=3, d=4, e=5, f=6, g=7)"
raw times: 1.02 1.07 1.03 1.11 1.07
1000000 loops, best of 5: 1.02 usec per loop

C:\Python33_64\python -m timeit -n 1000000 -r 5 -v
    "{'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7}"
raw times: 0.444 0.449 0.455 0.452 0.46
1000000 loops, best of 5: 0.444 usec per loop

Tested on Win7/64 bit. Python 2.7 is the 32-bit version, 3.2 is
installed as 32-bit and 64-bit versions and 3.3 only as 64-bit version.
In any case, the difference is even a bit stronger than what you
experience and it seems that all versions perform roughly similar.

Uli

*************************************************************************** ***********
Domino Laser GmbH, Fangdieckstra e 75a, 22547 Hamburg, Deutschland
Gesch ftsf hrer: Hans Robert Dapprich, Amtsgericht Hamburg HR B62 932
*************************************************************************** ***********
Visit our website at http://www.dominolaser.com
*************************************************************************** ***********
Diese E-Mail einschlie lich s mtlicher Anh nge ist nur f r den Adressaten bestimmt und kann vertrauliche Informationen enthalten. Bitte benachrichtigen Sie den Absender umgehend, falls Sie nicht der beabsichtigte Empf nger sein sollten. Die E-Mail ist in diesem Fall zu l schen und darf weder gelesen, weitergeleitet, ver ffentlicht oder anderweitig benutzt werden.
E-Mails k nnen durch Dritte gelesen werden und Viren sowie nichtautorisierte nderungen enthalten. Domino Laser GmbH ist f r diese Folgen nicht verantwortlich.
*************************************************************************** ***********

_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Withers  
View profile  
 More options Nov 14 2012, 5:02 am
From: Chris Withers <ch...@simplistix.co.uk>
Date: Wed, 14 Nov 2012 10:00:59 +0000
Local: Wed, Nov 14 2012 5:00 am
Subject: Re: [Python-Dev] performance of {} versus dict()
On 14/11/2012 09:58, Merlijn van Deen wrote:

> On 14 November 2012 10:12, Chris Withers <ch...@simplistix.co.uk> wrote:
>> ...which made me a little sad

> Why did it make you sad? dict() takes 0.2µs, {} takes 0.04µs. In other
> words: you can run dict() _five million_ times per second, and {}
> twenty-five million times per second. That is 'a lot' and 'a lot'. It
> also means you are unlikely to notice the difference in real-world
> code. Just use the one you feel is clearer in the situation, and don't
> worry about micro-optimalization.

I'm inclined to agree, but it makes me sad for two reasons:

- it's something that people get hung up on, for better or worse. (if it
wasn't, Doug wouldn't have written his article)

- it can make a difference, for example setting up a dict with many keys
at the core of a type loop.

Without looking at implementation, they should logically perform the same...

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mar...@v.loewis.de  
View profile  
 More options Nov 14 2012, 5:13 am
From: mar...@v.loewis.de
Date: Wed, 14 Nov 2012 11:11:39 +0100
Local: Wed, Nov 14 2012 5:11 am
Subject: Re: [Python-Dev] performance of {} versus dict()

Zitat von Chris Withers <ch...@simplistix.co.uk>:

> a_dict = dict(
>     x = 1,
>     y = 2,
>     z = 3,
>     ...
>     )
> What can we do to speed up the former case?

It should be possible to special-case it. Rather than creating
a new dictionary from scratch, one could try to have the new dictionary
the same size as the original one, and copy all entries.

I also wonder whether the PyArg_ValidateKeywordArguments call is really
necessary: if this is not a proper keyword dictionary, dict creation
could still proceed in a reasonable way.

I don't know how much this would gain, though. You still have to
create two dictionary objects. For a better speedup, try

def xdict(**kwds):
   return kwds

(possibly written in C for even more speed)

Regards,
Martin

_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Antoine Pitrou  
View profile  
 More options Nov 14 2012, 5:49 am
From: Antoine Pitrou <solip...@pitrou.net>
Date: Wed, 14 Nov 2012 11:48:09 +0100
Local: Wed, Nov 14 2012 5:48 am
Subject: Re: [Python-Dev] performance of {} versus dict()
Le Wed, 14 Nov 2012 10:00:59 +0000,
Chris Withers <ch...@simplistix.co.uk> a écrit :

Well, please post examples of *real-world* use cases where it makes a
difference.
Otherwise, you're asking us to add hacks to the implementation just to
make you feel good, which is quite unacceptable.

Regards

Antoine.

_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Nov 14 2012, 6:27 am
From: Steven D'Aprano <st...@pearwood.info>
Date: Wed, 14 Nov 2012 22:21:03 +1100
Local: Wed, Nov 14 2012 6:21 am
Subject: Re: [Python-Dev] performance of {} versus dict()
On 14/11/12 21:00, Chris Withers wrote:

> On 14/11/2012 09:58, Merlijn van Deen wrote:
>> On 14 November 2012 10:12, Chris Withers <ch...@simplistix.co.uk> wrote:
>>> ...which made me a little sad

>> Why did it make you sad? dict() takes 0.2µs, {} takes 0.04µs. In other
>> words: you can run dict() _five million_ times per second, and {}
>> twenty-five million times per second. That is 'a lot' and 'a lot'. It
>> also means you are unlikely to notice the difference in real-world
>> code. Just use the one you feel is clearer in the situation, and don't
>> worry about micro-optimalization.

> I'm inclined to agree, but it makes me sad for two reasons:

> - it's something that people get hung up on, for better or worse. (if it
>  wasn't, Doug wouldn't have written his article)

People get hung up on all sorts of things. I would hate to think we would
complicate the implementation to pander to pointless micro-optimization.
I'm sure that there are many far more important things than this.

> - it can make a difference, for example setting up a dict with many keys
>at the core of a type loop.

Ah yes, the semi-mythical "tight loop".

I've never come across one of these tight loops that creates a dict with
many keys in a tight loop, and then apparently fails to actually use it
for anything useful. For if it did, surely the actual work done with the
dict is going to outweigh the setup cost for all but the most trivial
applications. I find it hard to get uptight about a small inefficiency
in trivial applications that don't do much.

Show me a non-toy use-case where creating dicts is an actual bottleneck,
and I'll revise my position.

> Without looking at implementation, they should logically perform the same...

I disagree. Calling dict() has to do a name lookup, and then a function
call. That alone is almost 2.5 times as expensive as creating a dict
literal on my machine:

[steve@ando ~]$ python3.3 -m timeit "d = {}"
10000000 loops, best of 3: 0.17 usec per loop
[steve@ando ~]$ python3.3 -m timeit "d = dict()"
1000000 loops, best of 3: 0.416 usec per loop

Then you have the function call itself, which engages the argument parsing
mechanism, which does more work than dict literal syntax. For example, it
checks for duplicate keyword arguments, while dict literals happily accept
duplicate keys.

It's hardly a surprise that dict() is slower than {}.

--
Steven
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Nov 14 2012, 8:20 am
From: Chris Angelico <ros...@gmail.com>
Date: Thu, 15 Nov 2012 00:18:38 +1100
Local: Wed, Nov 14 2012 8:18 am
Subject: Re: [Python-Dev] performance of {} versus dict()

Perhaps an alternative question: What can be done to make the latter
less unpalatable? I personally prefer dict literal syntax to a dict
constructor call, but no doubt there are a number of people who feel
as you do. In what way(s) do you find the literal syntax less
readable, and can some simple (and backward-compatible) enhancements
help that?

I've seen criticisms (though I don't recall where) of Python,
comparing it to JavaScript/ECMAScript, that complain of the need to
quote the keys. IMO this is a worthwhile downside, as it allows you to
use variables as the keys, rather than requiring (effectively) literal
strings. But it does make a dict literal that much more "noisy" than
the constructor.

ChrisA
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Peterson  
View profile  
 More options Nov 14 2012, 8:44 am
From: Benjamin Peterson <benja...@python.org>
Date: Wed, 14 Nov 2012 08:43:18 -0500
Local: Wed, Nov 14 2012 8:43 am
Subject: Re: [Python-Dev] performance of {} versus dict()
2012/11/14  <mar...@v.loewis.de>:

In the common case PyArg_ValidateKeywordArguments should be a simple check.

--
Regards,
Benjamin
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stefan Behnel  
View profile  
 More options Nov 14 2012, 9:37 am
From: Stefan Behnel <stefan...@behnel.de>
Date: Wed, 14 Nov 2012 15:35:37 +0100
Local: Wed, Nov 14 2012 9:35 am
Subject: Re: [Python-Dev] performance of {} versus dict()
Chris Angelico, 14.11.2012 14:18:

If that bothers you in a specific case, I recommend using the constructor
instead of a literal.

Stefan

_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Adam  
View profile  
 More options Nov 14 2012, 11:16 am
From: Mark Adam <dreamingforw...@gmail.com>
Date: Wed, 14 Nov 2012 10:12:54 -0600
Local: Wed, Nov 14 2012 11:12 am
Subject: Re: [Python-Dev] performance of {} versus dict()

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
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Oleg Broytman  
View profile  
 More options Nov 14 2012, 11:24 am
From: Oleg Broytman <p...@phdru.name>
Date: Wed, 14 Nov 2012 20:20:55 +0400
Local: Wed, Nov 14 2012 11:20 am
Subject: Re: [Python-Dev] performance of {} versus dict()

   It doesn't. It's a call (function call or or a class instantiation)
and it's not dict-specific: function(a=1, b=None)...

Oleg.
--
     Oleg Broytman            http://phdru.name/            p...@phdru.name
           Programmers don't die, they just GOSUB without RETURN.
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Serhiy Storchaka  
View profile  
 More options Nov 14 2012, 11:25 am
From: Serhiy Storchaka <storch...@gmail.com>
Date: Wed, 14 Nov 2012 18:23:16 +0200
Local: Wed, Nov 14 2012 11:23 am
Subject: Re: [Python-Dev] performance of {} versus dict()
On 14.11.12 11:12, Chris Withers wrote:

PEP 8 recommends:

a_dict = dict(
     x=1,
     y=2,
     z=3,
     ...
)

and

a_dict = {
     'x': 1,
     'y': 2,
     'z': 3,
     ...

}

_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Oudkerk  
View profile  
 More options Nov 14 2012, 11:44 am
From: Richard Oudkerk <shibt...@gmail.com>
Date: Wed, 14 Nov 2012 16:42:39 +0000
Local: Wed, Nov 14 2012 11:42 am
Subject: Re: [Python-Dev] performance of {} versus dict()
On 14/11/2012 4:23pm, Serhiy Storchaka wrote:

In which section?  I can't see such a recommendation.

--
Richard

_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brian Curtin  
View profile  
 More options Nov 14 2012, 12:02 pm
From: Brian Curtin <br...@python.org>
Date: Wed, 14 Nov 2012 11:00:47 -0600
Local: Wed, Nov 14 2012 12:00 pm
Subject: Re: [Python-Dev] performance of {} versus dict()

What convention and typing optimization is this? I hope you aren't
suggesting it should be dict("x"=1) or dict("x":1)?
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Xavier Morel  
View profile  
 More options Nov 14 2012, 12:04 pm
From: Xavier Morel <python-...@masklinn.net>
Date: Wed, 14 Nov 2012 18:02:43 +0100
Local: Wed, Nov 14 2012 12:02 pm
Subject: Re: [Python-Dev] performance of {} versus dict()

On 2012-11-14, at 17:42 , Richard Oudkerk wrote:

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)

_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Adam  
View profile  
 More options Nov 14 2012, 12:09 pm
From: Mark Adam <dreamingforw...@gmail.com>
Date: Wed, 14 Nov 2012 11:08:32 -0600
Local: Wed, Nov 14 2012 12:08 pm
Subject: Re: [Python-Dev] performance of {} versus dict()

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

mark
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Adam  
View profile  
 More options Nov 14 2012, 12:11 pm
From: Mark Adam <dreamingforw...@gmail.com>
Date: Wed, 14 Nov 2012 11:10:15 -0600
Local: Wed, Nov 14 2012 12:10 pm
Subject: Re: [Python-Dev] performance of {} versus dict()

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
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
R. David Murray  
View profile  
 More options Nov 14 2012, 12:29 pm
From: "R. David Murray" <rdmur...@bitdance.com>
Date: Wed, 14 Nov 2012 12:27:46 -0500
Local: Wed, Nov 14 2012 12:27 pm
Subject: Re: [Python-Dev] performance of {} versus dict()

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

--David
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Adam  
View profile  
 More options Nov 14 2012, 12:59 pm
From: Mark Adam <dreamingforw...@gmail.com>
Date: Wed, 14 Nov 2012 11:57:48 -0600
Local: Wed, Nov 14 2012 12:57 pm
Subject: Re: [Python-Dev] performance of {} versus dict()
On Wed, Nov 14, 2012 at 11:27 AM, R. David Murray <rdmur...@bitdance.com> 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
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Oudkerk  
View profile  
 More options Nov 14 2012, 1:04 pm
From: Richard Oudkerk <shibt...@gmail.com>
Date: Wed, 14 Nov 2012 18:01:53 +0000
Local: Wed, Nov 14 2012 1:01 pm
Subject: Re: [Python-Dev] performance of {} versus dict()
On 14/11/2012 5:02pm, Xavier Morel wrote:

>> 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.

Oops, I did not even notice that difference.

I thought Serhiy was talking about indenting the closing ')' and '}'.

--
Richard

_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Xavier Morel  
View profile  
 More options Nov 14 2012, 1:10 pm
From: Xavier Morel <python-...@masklinn.net>
Date: Wed, 14 Nov 2012 19:08:44 +0100
Local: Wed, Nov 14 2012 1:08 pm
Subject: Re: [Python-Dev] performance of {} versus dict()
On 2012-11-14, at 18:08 , Mark Adam wrote:

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

And nobody said it was. It's a recommendation to not put spaces around
the equals sign when using keyword arguments which is the correction
Serhiy applied to the original code (along with adding a space after the
colon in the literal dict, also a PEP8 recommendation).
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Xavier Morel  
View profile  
 More options Nov 14 2012, 1:14 pm
From: Xavier Morel <catch-...@masklinn.net>
Date: Wed, 14 Nov 2012 19:12:25 +0100
Local: Wed, Nov 14 2012 1:12 pm
Subject: Re: [Python-Dev] performance of {} versus dict()
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.

>  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.
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Adam  
View profile  
 More options Nov 14 2012, 1:55 pm
From: Mark Adam <dreamingforw...@gmail.com>
Date: Wed, 14 Nov 2012 12:54:32 -0600
Local: Wed, Nov 14 2012 1:54 pm
Subject: Re: [Python-Dev] performance of {} versus dict()

On Wed, Nov 14, 2012 at 12:12 PM, Xavier Morel <catch-...@masklinn.net> 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
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Xavier Morel  
View profile  
 More options Nov 14 2012, 2:38 pm
From: Xavier Morel <catch-...@masklinn.net>
Date: Wed, 14 Nov 2012 20:37:02 +0100
Local: Wed, Nov 14 2012 2:37 pm
Subject: Re: [Python-Dev] performance of {} versus dict()
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.

> How do you do it on
> initialization?  This doesn't make sense.

dict(d1, **d2)
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Oleg Broytman  
View profile   Translate to Translated (View Original)
 More options Nov 14 2012, 8:25 am
From: Oleg Broytman <p...@phdru.name>
Date: Wed, 14 Nov 2012 17:25:00 +0400
Subject: Re: [Python-Dev] performance of {} versus dict()

   On the other had it's more powerful. You can write {'class': 'foo'}
but cannot dict(class='bar'). {1: '1'} but not dict(1='1').

Oleg.
--
     Oleg Broytman            http://phdru.name/            p...@phdru.name
           Programmers don't die, they just GOSUB without RETURN.
_______________________________________________
Python-Dev mailing list
Python-...@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/dev-python%2Bgarchi...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 66   Newer >
« Back to Discussions « Newer topic     Older topic »