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


Groups > comp.lang.python > #30123

Re: data attributes override method attributes?

From Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Newsgroups comp.lang.python
Subject Re: data attributes override method attributes?
Date 2012-09-25 21:52 +0200
Organization A newly installed InterNetNews server
Message-ID <k3t221$1vo$1@r03.glglgl.gl> (permalink)
References <931902e1-570b-4288-bb9b-de711318c5cd@googlegroups.com> <mailman.1338.1348582105.27098.python-list@python.org>

Show all headers | View raw


Am 25.09.2012 16:08 schrieb Peter Otten:
> Jayden wrote:
>
>> In the Python Tutorial, Section 9.4, it is said that
>>
>> "Data attributes override method attributes with the same name."
>
> The tutorial is wrong here. That should be
>
> "Instance attributes override class attributes with the same name."

I jump in here:

THere is one point to consider: if you work with descriptors, it makes a 
difference if they are "data descriptors" (define __set__ and/or 
__delete__) or "non-data descriptors" (define neither).

As http://docs.python.org/reference/datamodel.html#invoking-descriptors 
tells us, methods are non-data descriptors, so they can be overridden by 
instances.

OTOH, properties are data descriptors which cannot  be overridden by the 
instance.

So, to stick to the original example:

class TestDesc(object):
     def a(self): pass
     @property
     def b(self): print "trying to get value - return None"; return None
     @b.setter
     def b(self, v): print "value", v, "ignored."
     @b.deleter
     def b(self): print "delete called and ignored"


and now

 >>> t=TestDesc()
 >>> t.a
<bound method TestDesc.a of <__main__.TestDesc object at 0xb7387ccc>>
 >>> t.b
trying to get value - return None
 >>> t.a=12
 >>> t.b=12
value 12 ignored.
 >>> t.a
12
 >>> t.b
trying to get value - return None
 >>> del t.a
 >>> del t.b
delete called and ignored
 >>> t.a
<bound method TestDesc.a of <__main__.TestDesc object at 0xb7387ccc>>
 >>> t.b
trying to get value - return None


Thomas

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