Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > data attributes override method attributes?

Reply
Thread Tools

data attributes override method attributes?

 
 
Ian Kelly
Guest
Posts: n/a
 
      09-25-2012
On Tue, Sep 25, 2012 at 1:58 PM, Terry Reedy <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      09-25-2012
On 9/25/2012 10:54 AM, Peter Otten wrote:
> alex23 wrote:
>
>> On Sep 26, 12:08 am, Peter Otten <(E-Mail Removed)> wrote:
>>> 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."


Except for special methods.

> I would even consider replacing the whole paragraph


I agree
>
> """
> Data attributes override method attributes with the same name; to avoid
> accidental name conflicts, which may cause hard-to-find bugs in large
> programs, it is wise to use some kind of convention that minimizes the
> chance of conflicts. Possible conventions include capitalizing method names,
> prefixing data attribute names with a small unique string (perhaps just an
> underscore), or using verbs for methods and nouns for data attributes.
> """
> http://docs.python.org/dev/py3k/tutorial/classes.html
>
> with something like
>
> "Data attributes and method attributes share the same namespace.


and instance attributes usually override class attributes

> To avoid
> name conflicts consider using verbs for methods and nouns for data
> attributes"


This applies within and between. I opened

http://bugs.python.org/issue16048

--
Terry Jan Reedy

 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      09-25-2012
On 9/25/2012 4:07 PM, Ian Kelly wrote:
> On Tue, Sep 25, 2012 at 1:58 PM, Terry Reedy <(E-Mail Removed)> 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.


--
Terry Jan Reedy

 
Reply With Quote
 
Prasad, Ramit
Guest
Posts: n/a
 
      09-28-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 <(E-Mail Removed)> 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 areshadowed 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.
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      09-28-2012
On Fri, Sep 28, 2012 at 12:02 PM, Prasad, Ramit
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      09-28-2012
On 9/28/2012 2: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__()?


len(foo) calls someclass.__len__(foo) where someclass is foo.__class__or
some superclass.

> If so, why?


Efficiency and perhaps simpler implementation, especially for binary ops.

--
Terry Jan Reedy

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-29-2012
On Fri, 28 Sep 2012 18:02:04 +0000, 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__()?


If you call foo.__len__, the attribute lookup of __len__ will use the
exact same search path as foo.spam would use:

1) does __getattribute__ exist and intercept the call?
2) if not, does a instance attribute exist?
3) if not, does a class attribute exist?
4) if not, does a superclass attribute exist?
5) if not, does __getattr__ exist and intercept the call?

Using len(foo) bypasses steps 1) and 2) as a speed optimization. For the
common case where an instance's class directly defines a __len__ method,
that saves about 10-15% of the overhead of calling a special method,
compared to old-style classes.



--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Avoid derived class to override a method yet should allow callers toinvoke the method Ethan Java 4 09-07-2011 03:14 PM
Override method name and original method access Donn Ingle Python 0 11-12-2007 07:41 PM
class attributes & data attributes james_027 Python 2 06-20-2007 03:12 PM
Parse reserved attributes as normal attributes Max XML 1 09-22-2006 12:04 PM
Passing data attributes as method parameters Panos Laganakos Python 4 04-24-2006 09:55 AM



Advertisments