Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8052 > unrolled thread

Instances' __setitem__ methods

Started bySpencer Pearson <speeze.pearson@gmail.com>
First post2011-06-20 18:42 -0700
Last post2011-06-20 22:12 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Instances' __setitem__ methods Spencer Pearson <speeze.pearson@gmail.com> - 2011-06-20 18:42 -0700
    Re: Instances' __setitem__ methods Ethan Furman <ethan@stoneleaf.us> - 2011-06-20 20:37 -0700
    Re: Instances' __setitem__ methods Chris Rebert <clp2@rebertia.com> - 2011-06-20 22:12 -0700

#8052 — Instances' __setitem__ methods

FromSpencer Pearson <speeze.pearson@gmail.com>
Date2011-06-20 18:42 -0700
SubjectInstances' __setitem__ methods
Message-ID<13ffef25-879f-4d8f-8995-b4c358336c3d@o10g2000prn.googlegroups.com>
I was recently trying to implement a dict-like object which would do
some fancy stuff when it was modified, and found that overriding the
__setitem__ method of an instance did not act the way I expected. The
help documentation (from help(dict.__setitem__)) claims that
"d.__setitem__(k,v)" is equivalent to "d[k]=v", but I've produced this
code that, on Python 2.6, acts differently in the two cases.

def print_args( key, value ):
    print "print_args called: key = %s, value = %s" %(key,value)

class MyDict( dict ):
    def __init__( self ):
        dict.__init__( self )
        self.__setitem__ = print_args

    def __setitem__( self, key, value ):
        print "ModelDict.__setitem__ called"
        dict.__setitem__( self, key, value )

d = MyDict()

print "d.__setitem__(0,1):",
d.__setitem__(0,1)

print "d[0]=1:",
d[0]=1


I would expect the two setitems to both call print_args, but that's
not what happens. In the first case, it calls print_args, but in the
second case, the __setitem__ declared in MyDict is called instead.

The documentation at http://docs.python.org/reference/datamodel.html#specialnames
says that for new-style classes, "x[i]" is equivalent to
"type(x).__getitem__(x, i)". I assume that "x[i]=y" has similarly been
changed to be equivalent to "type(x).__setitem__(x, i, y)", since that
would produce the results that I'm getting. Is the help documentation
for dict.__setitem__ just outdated, or am I missing some subtlety
here?

Also: when I say "d.f(*args)", am I correct in thinking that d checks
to see if it has an instance attribute called "f", and if it does,
calls f(*args); and if it doesn't, checks whether its parent class
(and then its grandparent, and so on) has a class attribute called
"f", and if it does, calls f(x, *args)?

[toc] | [next] | [standalone]


#8064

FromEthan Furman <ethan@stoneleaf.us>
Date2011-06-20 20:37 -0700
Message-ID<mailman.214.1308627516.1164.python-list@python.org>
In reply to#8052
Spencer Pearson wrote:
> I was recently trying to implement a dict-like object which would do
> some fancy stuff when it was modified, and found that overriding the
> __setitem__ method of an instance did not act the way I expected. 

The __magic__ methods are only looked up on the class, never the instance.

~Ethan~

[toc] | [prev] | [next] | [standalone]


#8071

FromChris Rebert <clp2@rebertia.com>
Date2011-06-20 22:12 -0700
Message-ID<mailman.215.1308633180.1164.python-list@python.org>
In reply to#8052
On Mon, Jun 20, 2011 at 6:42 PM, Spencer Pearson
<speeze.pearson@gmail.com> wrote:
> I was recently trying to implement a dict-like object which would do
> some fancy stuff when it was modified, and found that overriding the
> __setitem__ method of an instance did not act the way I expected. The
> help documentation (from help(dict.__setitem__)) claims that
> "d.__setitem__(k,v)" is equivalent to "d[k]=v", but I've produced this
> code that, on Python 2.6, acts differently in the two cases.

Technically, the strict equivalence is only one-way, as you've shown;
but one generally avoids calling the __magic__ methods directly, so
this subtle distinction is seldom used intentionally.

<snip>
> I would expect the two setitems to both call print_args, but that's
> not what happens. In the first case, it calls print_args, but in the
> second case, the __setitem__ declared in MyDict is called instead.
>
> The documentation at http://docs.python.org/reference/datamodel.html#specialnames
> says that for new-style classes, "x[i]" is equivalent to
> "type(x).__getitem__(x, i)". I assume that "x[i]=y" has similarly been
> changed to be equivalent to "type(x).__setitem__(x, i, y)", since that
> would produce the results that I'm getting. Is the help documentation
> for dict.__setitem__ just outdated, or am I missing some subtlety
> here?

The sentence of the docs in question begins with "For instance,";
hence, __setitem__ is just the arbitrarily-chosen example used in this
part of the docs. But the point applies equally to all the special
methods. See the very last section of the same webpage:
http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes

> Also: when I say "d.f(*args)", am I correct in thinking that d checks
> to see if it has an instance attribute called "f", and if it does,
> calls f(*args); and if it doesn't, checks whether its parent class
> (and then its grandparent, and so on) has a class attribute called
> "f", and if it does, calls f(x, *args)?

See the first paragraph under the "Classes" entry on
http://docs.python.org/reference/datamodel.html#objects-values-and-types
for perfect accuracy.

Cheers,
Chris

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web