default behavior

W

Wolfram Hinderer

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/reference/datamodel.html#special-method-names

However, "normal" special method lookup seems to be used.
 
J

John Posner

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/reference/datamodel.html#special-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
 
S

Stefan Schwarzer

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

David Niergarth

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

and properties
1.0

Curiously, this works
 
D

David Niergarth

[Oops, now complete...]
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

but not an int.
File "<stdin>", line 1
1.conjugate()
^
SyntaxError: invalid syntax

Anyway, I didn't realize int has a method you can call.

--David
 
P

Peter Otten

David said:
[Oops, now complete...]
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.
 
S

Steven D'Aprano

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.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, AmsterdamTraceback (innermost last):
File "<stdin>", line 1, in ?
AttributeError: 'int' object has no attribute '__add__'

I see other instance examples also work.

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

Piet van Oostrum

Steven D'Aprano said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top