Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'attribute': 0.05; 'attributes': 0.07; 'called.': 0.09; 'lambda:': 0.09; 'lookup': 0.09; 'sep': 0.09; 'subject:method': 0.09; 'def': 0.10; 'attributes,': 0.16; 'foo()': 0.16; 'foo(object):': 0.16; 'wrote:': 0.17; 'instance': 0.17; '>>>': 0.18; 'define': 0.20; 'received:mail-bk0-f46.google.com': 0.22; 'class.': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'implemented': 0.27; 'received:209.85.214.46': 0.27; 'message- id:@mail.gmail.com': 0.27; 'case,': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'fri,': 0.30; 'returned': 0.30; 'code': 0.31; 'goes': 0.33; 'instances': 0.33; 'subject:data': 0.33; 'to:addr :python-list': 0.33; 'received:google.com': 0.34; 'so,': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'method': 0.36; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'first': 0.61; 'for:': 0.64; 'to:name:python': 0.84; 'why?': 0.84; 'prescribed': 0.91; 'shadow': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=mvqFvvIawJhEnVbzgHAtMi6ZLhnSvmtOd8mAjoOF9QU=; b=oaltPl6MLzbyQKnIzTtJsgYWFvywu5qcFfPzS0CVZes3V9VvxRMIWmIEvDVxIPneo8 mi+niyiJ9cz6soFJQ9dWMvTlhoZL85mThD3BQio3Uzttk64FSx6EBTz/T3dnN+63h/R+ 2YjBWPb6uvyGMMeg4U94ODc21U8cnM9k/8ZljzJ1wMQLW89p/VU6xgv1hn6ZdFOvd8Cm 0LwwB+Q+kHKVM37v60FFrz2EpMnTRc4857v4l7c2Il5BlHShZ/5Zne1W9Eyianif9rhq StQHukaMyZU7ChvA7neeCYYoZ3V/UOpMJxbQO7SqlhH4oHCkZ+RwAsH4qzl7X3f/EW2O DInw== MIME-Version: 1.0 In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474166CCB67@SCACMX008.exchad.jpmchase.net> References: <931902e1-570b-4288-bb9b-de711318c5cd@googlegroups.com> <5B80DD153D7D744689F57F4FB69AF474166CCB67@SCACMX008.exchad.jpmchase.net> From: Ian Kelly Date: Fri, 28 Sep 2012 12:25:11 -0600 Subject: Re: data attributes override method attributes? To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348856743 news.xs4all.nl 6939 [2001:888:2000:d::a6]:39935 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30422 On Fri, Sep 28, 2012 at 12:02 PM, Prasad, Ramit wrote: > Just to make sure I am following, if you call > foo.__len__() it goes to the instance code while > if you do len(foo) it will go to class.__len__()? Yes: >>> class Foo(object): ... def __len__(self): ... return 42 ... >>> foo = Foo() >>> foo.__len__ = lambda: 43 >>> foo.__len__() 43 >>> len(foo) 42 > If so, why? In the first case, "foo.__len__" just does the normal attribute lookup for the class. Instance attributes shadow class attributes, so the instance attribute is returned and then called. In the second case, "len(foo)" is implemented by a method in a prescribed location: foo.__class__.__len__. It only looks in the class for efficiency and because that is what the class object is for: to define how its instances behave.