Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Inheritance from builtin list and override of methods.

Reply
Thread Tools

Re: Inheritance from builtin list and override of methods.

 
 
Michalis Giannakidis
Guest
Posts: n/a
 
      11-27-2006
On Monday 27 November 2006 11:50, Fredrik Lundh wrote:

> "obj[index] = value" maps to "obj.__setitem__(index, value)". reading
> the documentation might help; start here:
>
> http://docs.python.org/ref/specialnames.html


In this documentation page it also says:
--snip---
then x[i] is equivalent3.2 to x.__getitem__(i).
--snip--
This, and other statements, are only roughly true for instances of new-style
classes.
--snip--

So which statements are actually true for new style classes?

Is l[0]=1 completely different from l.append(1) or maybe insert?

And is there a mechanism in Python that will allow me to override the
operators of a class, for all its occurrences, even the ones implemented on C
built-in objects?


--
Michalis Giannakidis
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      11-27-2006
Michalis Giannakidis <(E-Mail Removed)> wrote:

> On Monday 27 November 2006 11:50, Fredrik Lundh wrote:
>
>> "obj[index] = value" maps to "obj.__setitem__(index, value)".
>> reading the documentation might help; start here:
>>
>> http://docs.python.org/ref/specialnames.html

>
> In this documentation page it also says:
> --snip---
> then x[i] is equivalent3.2 to x.__getitem__(i).
> --snip--
> This, and other statements, are only roughly true for instances of
> new-style classes.
> --snip--
>
> So which statements are actually true for new style classes?


I think it means that the statements are true in general, but there may be
specific corner cases which break them. Usually you can assume that the
rough description will be good enough.

>
> Is l[0]=1 completely different from l.append(1) or maybe insert?


Yes. It neither appends nor inserts.

> And is there a mechanism in Python that will allow me to override the
> operators of a class, for all its occurrences, even the ones
> implemented on C built-in objects?


No.

 
Reply With Quote
 
 
 
 
OKB (not okblacke)
Guest
Posts: n/a
 
      11-27-2006
Duncan Booth wrote:

>> And is there a mechanism in Python that will allow me to override
>> the operators of a class, for all its occurrences, even the ones
>> implemented on C built-in objects?

>
> No.


For what it's worth, which is undoubtedly nothing, this is
something that I think needs to change. All this talk about new-style
classes and class-type unification is empty words if you can't override
anything on any type without having to know whether it's written in C or
Python.

--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
 
Reply With Quote
 
Carsten Haese
Guest
Posts: n/a
 
      11-27-2006
On Mon, 2006-11-27 at 19:14 +0000, OKB (not okblacke) wrote:
> Duncan Booth wrote:
>
> >> And is there a mechanism in Python that will allow me to override
> >> the operators of a class, for all its occurrences, even the ones
> >> implemented on C built-in objects?

> >
> > No.

>
> For what it's worth, which is undoubtedly nothing,


Correct.

> this is
> something that I think needs to change. All this talk about new-style
> classes and class-type unification is empty words if you can't override
> anything on any type without having to know whether it's written in C or
> Python.


Duncan's "No" response was not referring to overriding in general, it
was referring to the OP's original question which amounted to "Can I
affect the behavior of method Z by overriding methods X and Y".

The inability to influence a method by overriding completely different
methods has nothing to do with whether that method is implemented in C
or Python; it has to with whether the method in question calls the
overridden methods, and in general it won't.

You can change the behavior of a list's sort method by overriding sort.
You can't change the behavior of sort by overriding __getitem__ and
__setitem__, because sort does not call __getitem__ or __setitem__.

Hope this helps,

Carsten.


 
Reply With Quote
 
OKB (not okblacke)
Guest
Posts: n/a
 
      11-28-2006
Carsten Haese wrote:

> You can change the behavior of a list's sort method by overriding
> sort. You can't change the behavior of sort by overriding
> __getitem__ and __setitem__, because sort does not call __getitem__
> or __setitem__.


Why doesn't it?

--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-28-2006
OKB (not okblacke) wrote:

> Why doesn't it?


because whoever wrote the class didn't do things that way, mostly for
efficiency reasons. there's nothing in Python that keeps you from using
template methods if you want, but that's a costly approach, so it's not
very common.

I suggest reading up on OO design patterns.

</F>

 
Reply With Quote
 
Michalis Giannakidis
Guest
Posts: n/a
 
      11-28-2006
On Tuesday 28 November 2006 06:12, OKB (not okblacke) wrote:
> Carsten Haese wrote:
> > You can change the behavior of a list's sort method by overriding
> > sort. You can't change the behavior of sort by overriding
> > __getitem__ and __setitem__, because sort does not call __getitem__
> > or __setitem__.

>
> Why doesn't it?


I also expected that it did!

I perfectly understand that this adds significant penalty to the execution of
code. But in the way things are, I have to know ( or guess ?) how its
function has been implemented. And I cannot handle all cases the same.

If I provided my sort method for my class in Python, wouldn't this call my
__getitem__ and __setitem__ methods (considering l[i] = j assignment do)? Can
this be considered an in consitency?

--
Michalis Giannakidis
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-28-2006
Michalis Giannakidis wrote:

> I perfectly understand that this adds significant penalty to the execution of
> code. But in the way things are, I have to know ( or guess ?) how its
> function has been implemented.


and that's different from how object orientation usually works in
exactly what way?

</F>

 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      11-28-2006
OKB (not okblacke) wrote:
> Carsten Haese wrote:
>
> > You can change the behavior of a list's sort method by overriding
> > sort. You can't change the behavior of sort by overriding
> > __getitem__ and __setitem__, because sort does not call __getitem__
> > or __setitem__.

>
> Why doesn't it?


Because the concerns of thousands of legitimate programmers who want
good performance out of their sorts outweigh the concerns of the one or
two hax0r d00ds who think it would be cool to hook into sort internals.


live-long-and-prosper-ly yr's,


Carl Banks

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-28-2006
Carl Banks wrote:

> Because the concerns of thousands of legitimate programmers who want
> good performance out of their sorts outweigh the concerns of the one or
> two hax0r d00ds who think it would be cool to hook into sort internals.


.... and haven't yet realized that using sorted() and converting back
from the resulting list will outperform *any* solution they can come
up with themselves.

</F>

 
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
subtle error slows code by 10x (builtin sum()) - replace builtin sumwithout using import? bdb112 Python 2 07-02-2011 03:13 AM
Inheritance from builtin list and override of methods. Michalis Giannakidis Python 0 11-26-2006 04:04 PM
A memory leak involving builtin/pure-python inheritance. nejucomo@gmail.com Python 0 11-14-2006 06:58 AM
Using inheritance for web pages - why have a Page_Init when you can override OnInit? ASP .Net 2 07-16-2003 07:32 PM
Using inheritance for web pages - why have a Page_Init when you can override OnInit? ASP .Net 2 07-13-2003 04:07 AM



Advertisments