From: Stefan Schwarzer on 7 Aug 2010 06:41 On 2010-07-31 05:47, Steven D'Aprano wrote: > On Fri, 30 Jul 2010 08:34:52 -0400, wheres pythonmonks wrote: > It does re-use the same underlying data. > > >>> from collections import defaultdict as dd > >>> x = dd(list) > >>> x[1].append(1) > >>> x > defaultdict(<type 'list'>, {1: [1]}) > >>> y = dict(x) > >>> x[1].append(42) > >>> y > {1: [1, 42]} One thing to keep in mind: dict(some_defaultdict) doesn't store a reference to the defaultdict; instead it makes a shallow copy, so key/value pairs added _after_ the "cast" aren't included in the new dict: >>> y[2] = 17 >>> y {1: [1, 42], 2: 17} >>> x defaultdict(<type 'list'>, {1: [1, 42]}) Stefan
From: David Niergarth on 12 Aug 2010 16:28 Peter Otten <__pete...(a)web.de> wrote: > > >>> 1 .conjugate() > This is a syntax I never noticed before. My built-in complier (eyes) took one look and said: "that doesn't work." Has this always worked in Python but I never noticed? I see other instance examples also work. >>> '1' .zfill(2) '01' >>> 1.0 .is_integer() True and properties >>> 1.0 .real 1.0 Curiously, this works
From: David Niergarth on 12 Aug 2010 16:37 [Oops, now complete...] Peter Otten <__pete...(a)web.de> wrote: > > > >>> 1 .conjugate() > This is a syntax I never noticed before. My built-in complier (eyes) took one look and said: "that doesn't work." Has this always worked in Python but I never noticed? I see other instance examples also work. >>> '1' .zfill(2) '01' >>> 1.0 .is_integer() True and properties >>> 1.0 .real 1.0 Curiously, a float literal works without space >>> 1.0.conjugate() 1.0 but not an int. >>> 1.conjugate() File "<stdin>", line 1 1.conjugate() ^ SyntaxError: invalid syntax Anyway, I didn't realize int has a method you can call. --David
From: Peter Otten on 12 Aug 2010 16:52 David Niergarth wrote: > [Oops, now complete...] > Peter Otten <__pete...(a)web.de> wrote: >> >> > >>> 1 .conjugate() >> > This is a syntax I never noticed before. My built-in complier (eyes) > took one look and said: "that doesn't work." (1).conjugate may hurt a little less. Anyway, the space is only needed for the tokenizer that without it would produce a float immediately followed by a name. > Has this always worked in > Python but I never noticed? Probably. > I see other instance examples also work. > > >>> '1' .zfill(2) > '01' > >>> 1.0 .is_integer() > True > > and properties > > >>> 1.0 .real > 1.0 > > Curiously, a float literal works without space > > >>> 1.0.conjugate() > 1.0 > > but not an int. > > >>> 1.conjugate() > File "<stdin>", line 1 > 1.conjugate() > ^ > SyntaxError: invalid syntax > > Anyway, I didn't realize int has a method you can call. > > --David
From: Steven D'Aprano on 13 Aug 2010 01:51
On Thu, 12 Aug 2010 13:28:26 -0700, David Niergarth wrote: > Peter Otten <__pete...(a)web.de> wrote: >> >> >>> 1 .conjugate() >> >> > This is a syntax I never noticed before. My built-in complier (eyes) > took one look and said: "that doesn't work." Has this always worked in > Python but I never noticed? Yes. Here is is working in Python 2.2: [steve(a)sylar ~]$ python2.2 Python 2.2.3 (#1, Aug 12 2010, 01:08:27) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 2 .__add__(3) 5 Syntactically, it also worked as far back as Python 1.5, although it is rather pointless since int objects didn't gain any methods until 2.2: [steve(a)sylar ~]$ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> 2 .__add__(3) Traceback (innermost last): File "<stdin>", line 1, in ? AttributeError: 'int' object has no attribute '__add__' > I see other instance examples also work. > > >>> '1' .zfill(2) > '01' You don't need the space between strings and the attribute access: "1".zfill(2) is fine. You only need it for numbers, due to the ambiguity between the decimal point and dotted attribute access. -- Steven |