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


Groups > comp.lang.python > #30422

Re: data attributes override method attributes?

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 <ian.g.kelly@gmail.com>
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> <mailman.1338.1348582105.27098.python-list@python.org> <c3fb0a89-abb9-4107-89cd-3f4ee17b40e1@t2g2000pbt.googlegroups.com> <k3sgio$9v$1@ger.gmane.org> <CAPTjJmo9A2_0hjy2RJW+ALAvd4CTmUfOfh8sy7LspSJxdiWPgw@mail.gmail.com> <k3t2di$9f6$1@ger.gmane.org> <CALwzidk+8BftxXffxidEBZn9xwq6bDMAcTa81HYpq8jGfE=BqA@mail.gmail.com> <k3t4gj$ral$1@ger.gmane.org> <5B80DD153D7D744689F57F4FB69AF474166CCB67@SCACMX008.exchad.jpmchase.net>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 28 Sep 2012 12:25:11 -0600
Subject Re: data attributes override method attributes?
To Python <python-list@python.org>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1577.1348856743.27098.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Fri, Sep 28, 2012 at 12:02 PM, Prasad, Ramit
<ramit.prasad@jpmorgan.com> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

data attributes override method attributes? Jayden <jayden.shui@gmail.com> - 2012-09-25 06:41 -0700
  Re: data attributes override method attributes? alex23 <wuwei23@gmail.com> - 2012-09-25 06:52 -0700
  Re: data attributes override method attributes? Peter Otten <__peter__@web.de> - 2012-09-25 16:08 +0200
    Re: data attributes override method attributes? alex23 <wuwei23@gmail.com> - 2012-09-25 07:11 -0700
      Re: data attributes override method attributes? Peter Otten <__peter__@web.de> - 2012-09-25 16:54 +0200
      Re: data attributes override method attributes? Chris Angelico <rosuav@gmail.com> - 2012-09-26 01:03 +1000
      Re: data attributes override method attributes? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-09-25 16:54 +0200
      Re: data attributes override method attributes? Terry Reedy <tjreedy@udel.edu> - 2012-09-25 15:58 -0400
      Re: data attributes override method attributes? Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-25 14:07 -0600
      Re: data attributes override method attributes? Terry Reedy <tjreedy@udel.edu> - 2012-09-25 16:18 -0400
      Re: data attributes override method attributes? Terry Reedy <tjreedy@udel.edu> - 2012-09-25 16:34 -0400
      RE: data attributes override method attributes? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-09-28 18:02 +0000
        Re: data attributes override method attributes? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-29 03:20 +0000
      Re: data attributes override method attributes? Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-28 12:25 -0600
      Re: data attributes override method attributes? Terry Reedy <tjreedy@udel.edu> - 2012-09-28 14:26 -0400
    Re: data attributes override method attributes? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-25 21:52 +0200
  Re: data attributes override method attributes? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-25 14:12 +0000

csiph-web