data attributes override method attributes?

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Sep 28 14:02:04 EDT 2012


Terry Reedy wrote:
> On 9/25/2012 4:07 PM, Ian Kelly wrote:
> > On Tue, Sep 25, 2012 at 1:58 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> >> On 9/25/2012 11:03 AM, Chris Angelico wrote:
> >>> Instance attributes override (shadow) class attributes.
> >>
> >>
> >> except for (some? all?) special methods
> >
> > Those names are shadowed too.  If you call foo.__len__() and the name
> > is bound on the instance, it will call that function preferentially.
> > It's just that when the special Python machinery calls the method, it
> > skips the instance and goes straight to the class.
> 
> I added "Ian Kelly reminds me that instance.__xxx__ is only skipped by
> the internal machinery and not by direct accesses in user code. In the
> other hand, docs, official or otherwise, are filled with things like
> 'len(a) calls a.__len__', so I think something should be said that
> giving instances special method attributes does not have the effect one
> might expect."
> 
> to the issue.
> 


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__()?

If so, why? 

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list