Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'instance': 0.05; 'distinction': 0.07; 'python': 0.08; 'also:': 0.09; 'attribute': 0.09; 'pearson': 0.09; 'pm,': 0.10; 'wrote:': 0.14; '2.6,': 0.16; 'docs.': 0.16; 'does,': 0.16; 'expected.': 0.16; 'fancy': 0.16; 'getting.': 0.16; 'happens.': 0.16; 'new-style': 0.16; 'subtlety': 0.16; "subject:' ": 0.16; 'cc:addr:python-list': 0.17; 'mon,': 0.17; 'classes,': 0.19; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'cc:2**0': 0.22; 'stuff': 0.22; 'assume': 0.23; 'cc:no real name:2**0': 0.23; 'trying': 0.23; 'equally': 0.23; 'here?': 0.23; 'similarly': 0.23; 'code': 0.24; '(and': 0.25; 'changed': 0.25; 'expect': 0.25; 'produced': 0.26; 'received:209.85.161': 0.26; 'object': 0.26; 'says': 0.27; 'example': 0.27; "i'm": 0.27; 'correct': 0.28; 'message-id:@mail.gmail.com': 0.28; 'class': 0.29; 'implement': 0.30; 'cc:addr:python.org': 0.30; 'second': 0.30; 'cases.': 0.30; 'differently': 0.30; 'paragraph': 0.30; 'parent': 0.30; 'entry': 0.31; 'equivalent': 0.31; 'calling': 0.31; 'generally': 0.33; 'skip:" 20': 0.33; "i've": 0.33; 'question': 0.34; 'chris': 0.34; 'thinking': 0.34; 'that,': 0.34; 'skip:" 10': 0.35; 'begins': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.37; '20,': 0.37; 'checks': 0.37; 'instead.': 0.37; 'url:docs': 0.37; 'two': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'but': 0.38; 'docs': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'case,': 0.39; 'called': 0.39; 'received:209': 0.39; 'under': 0.40; 'missing': 0.40; 'help': 0.40; 'results': 0.60; 'perfect': 0.64; 'act': 0.65; 'special': 0.66; '"for': 0.67; 'acts': 0.77; 'claims': 0.82; "doesn't,": 0.84; 'seldom': 0.84; 'sender:addr:chris': 0.84; 'hence,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=juGx25orgIdZl+/tRuwYIl1dRpOUMtNywR4NtrXJNVU=; b=N2pWPv+ujiBoJcMuZtovMPa1aW1p27IMHPouOR7NABN+t77jfIEXDmFh2UhrDGdjJU Bk9r+NE1Oi6G/O/OYrpYUe4TwUZGDvxwX3QOgdsFtpBzv9kAHmqfKAibJDaILNBQphAN Mkf3xnATP2zE+H6QQhWWNVeslP5bDKCWOZ8zM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; b=eS498fOdzHZt2RJLcTFkScHoUaEcDeupEsXQs1AcHMmkuTKzFzmDxMXqOIwemabHmb VdGtexocs7AV5AMM2r8JEr5YWQ05r85kvqpxSimFj3W+xN/I3+MiNHcD1UwzVx9TaN3d k3aJ8zfJzhx9bjRkCVql2tXjrHBamB9ws98b8= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <13ffef25-879f-4d8f-8995-b4c358336c3d@o10g2000prn.googlegroups.com> References: <13ffef25-879f-4d8f-8995-b4c358336c3d@o10g2000prn.googlegroups.com> Date: Mon, 20 Jun 2011 22:12:51 -0700 X-Google-Sender-Auth: NUwvQAak8XiNUsqHmvYKGNjAEcc Subject: Re: Instances' __setitem__ methods From: Chris Rebert To: Spencer Pearson Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 44 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308633180 news.xs4all.nl 49184 [::ffff:82.94.164.166]:40446 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8071 On Mon, Jun 20, 2011 at 6:42 PM, 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 > 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. > 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