Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > default behavior

Reply
Thread Tools

default behavior

 
 
Wolfram Hinderer
Guest
Posts: n/a
 
      08-06-2010
On 6 Aug., 22:07, John Posner <(E-Mail Removed)> wrote:
> On 8/2/2010 11:00 PM, John Posner wrote:
>
> > On 7/31/2010 1:31 PM, John Posner wrote:

>
> >> Caveat -- there's another description of defaultdict here:

>
> >>http://docs.python.org/library/colle...ns.defaultdict

>
> >> ... and it's bogus. This other description claims that __missing__ is a
> >> method of defaultdict, not of dict.

>
> > Following is a possible replacement for the bogus description. Comments
> > welcome. I intend to submit a Python doc bug, and I'd like to have a
> > clean alternative to propose.

>
> After some off-list discussion with Ethan Furman (many thanks!), the
> Python Doc bug is submitted: #9536 at bugs.python.org.
>
> -John


This is probably nitpicking, but the patch calls __missing__ a special
method. However, unlike special methods, it is not invoked by "special
syntax" but by the dict's __getitem__ method. (len() invokes __len__
on any object - you can't do something similar with __missing__.)

__missing__ is also not listed as a special method on
http://docs.python.org/py3k/referenc...l-method-names

However, "normal" special method lookup seems to be used.
 
Reply With Quote
 
 
 
 
John Posner
Guest
Posts: n/a
 
      08-06-2010
On 8/6/2010 6:24 PM, Wolfram Hinderer wrote:
>
> This is probably nitpicking, but the patch calls __missing__ a special
> method. However, unlike special methods, it is not invoked by "special
> syntax" but by the dict's __getitem__ method. (len() invokes __len__
> on any object - you can't do something similar with __missing__.)
>
> __missing__ is also not listed as a special method on
> http://docs.python.org/py3k/referenc...l-method-names
>
> However, "normal" special method lookup seems to be used.


Fair enough. Please add your comment to #9536 at bugs.python.org.

Tx,
John
 
Reply With Quote
 
 
 
 
Stefan Schwarzer
Guest
Posts: n/a
 
      08-07-2010
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
 
Reply With Quote
 
David Niergarth
Guest
Posts: n/a
 
      08-12-2010
Peter Otten <(E-Mail Removed)> 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

 
Reply With Quote
 
David Niergarth
Guest
Posts: n/a
 
      08-12-2010
[Oops, now complete...]
Peter Otten <(E-Mail Removed)> 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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      08-12-2010
David Niergarth wrote:

> [Oops, now complete...]
> Peter Otten <(E-Mail Removed)> 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


 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-13-2010
On Thu, 12 Aug 2010 13:28:26 -0700, David Niergarth wrote:

> Peter Otten <(E-Mail Removed)> 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@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@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
 
Reply With Quote
 
Piet van Oostrum
Guest
Posts: n/a
 
      12-08-2010
Steven D'Aprano <(E-Mail Removed)> writes:

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


Personally I prefer parentheses: (1).conjugate
--
Piet van Oostrum <(E-Mail Removed)>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
Nu Fair Trade woonartikelen op http://www.zylja.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
importhook default behavior Pete Shinners Python 0 06-17-2004 02:51 PM
Strange behavior with default function assignment Denis Remezov C++ 4 06-08-2004 05:39 PM
Strange Behavior on Default Web page Stephen F Zelonis ASP .Net 0 04-05-2004 10:49 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM
Disabling default button behavior Stephen Walch ASP .Net 1 11-03-2003 07:13 PM



Advertisments