Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'indicating': 0.07; 'python3': 0.07; 'constructor': 0.09; 'subtle': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'creates': 0.14; '(k,': 0.16; '(note': 0.16; '-tkc': 0.16; 'colons,': 0.16; 'commas,': 0.16; 'foo()': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'kwargs': 0.16; 'one-element': 0.16; 'received:174.136': 0.16; 'set()': 0.16; 'subject:Java': 0.16; 'surprising': 0.16; 'tuple,': 0.16; 'tuple.': 0.16; "{'a':": 0.16; 'wrote:': 0.18; 'feb': 0.22; '>>>': 0.22; 'cc:addr:python.org': 0.22; '(a)': 0.24; "aren't": 0.24; 'instead.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'subject:/': 0.26; 'header:In-Reply-To:1': 0.27; 'related': 0.29; 'andrew': 0.30; 'tuples': 0.31; 'linux': 0.33; 'created': 0.35; 'but': 0.35; 'charset:us-ascii': 0.36; 'received:10': 0.37; 'ian': 0.60; "you're": 0.61; 'more': 0.64; '2014,': 0.84; 'dict()': 0.84; 'dict.': 0.84; 'subject:experience': 0.84; '2013,': 0.91 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1430774745947:741616142 X-MC-Ingress-Time: 1430774745947 Date: Mon, 4 May 2015 16:26:30 -0500 From: Tim Chase To: Andrew Cooper Cc: python-list@python.org Subject: Re: Bitten by my C/Java experience In-Reply-To: References: <87r3qwid3u.fsf@Equus.decebal.nl> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AuthUser: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430777138 news.xs4all.nl 2934 [2001:888:2000:d::a6]:37063 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89940 On 2015-05-04 21:57, Andrew Cooper wrote: > On 04/05/2015 18:43, Ian Kelly wrote: > > > > Some other gotchas that aren't necessarily related to C/Java but > > can be surprising nonetheless: > > > > * () is a zero-element tuple, and (a, b) is a two-element > > tuple, but (a) is not a one-element tuple. Tuples are created by > > commas, not parentheses, so use (a,) instead. > > * {} is an empty set(), not dict(). > > Particularly subtle when combined with **kwargs > > $ python3 > Python 3.4.0 (default, Apr 11 2014, 13:05:11) > [GCC 4.8.2] on linux > Type "help", "copyright", "credits" or "license" for more > information. > >>> def foo(**kwargs): > ... return { (k, kwargs[k]) for k in kwargs } > ... > >>> foo() > set() > >>> foo(a=1) > {('a', 1)} > >>> It's a dict: Python 3.2.3 (default, Feb 20 2013, 14:44:27) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> type({}) What you're seeing is that your generator creates single-element tuples in a set constructor (note that your last item isn't "{'a': 1}". Try instead >>> def foo(**kwargs): ... return {k: kwargs[k] for k in kwargs} ... >>> foo() {} >>> foo(a=42) {'a': 42} Note the colons, indicating that it's a dict. You're using the dict() syntax: dict((k,v) for k,v in some_iter()) -tkc