My own list object

Cesar Douady cesar.douady at asim.lip6.fr
Tue Feb 5 13:33:06 EST 2002


In article <a3p2fg$49g$1 at mail1.wg.waii.com>, "Martin Franklin"
<martin.franklin at westgeo.com> wrote:

> MDK wrote:
> 
> 
>> "Emile van Sebille" <emile at fenx.com> wrote in message
>> news:a3p0ss$19ob7s$1 at ID-11957.news.dfncis.de...
>>>
>>> "MDK" <mdk at mdk.com> wrote in message
>>> news:a3p01b$186eq8$1 at ID-98166.news.dfncis.de...
>>> > Hello,
>>> >
>>> > I am trying to create my own special type of list object.
>>> >
>>> > However, when I do x.append('yo') it uses Python's append instead of
>>> the one
>>> > from my class.
>>> >
>>> > How can I get it to use my append method?  I have tried def
>>> > __append__
>>> and
>>> > then called my own class' function but that did not work.  I've
>>> > played
>>> with
>>> > __getattr__ but that did not help.
>>> >
...
>> 
> Post some code.  perhaps the answer is obvious...
> 
> 
> for example
> 
> 
>>>> class MyList:
> ...     def __init__(self):
> ...             self.list=[]
> ...     def append(self, item):
> ...             self.list.append(item) ...
>>>> ml=MyList()
>>>> ml.append(1)
>>>> ml.list
> [1]
>>>> 
>>>> 
> would apear to work?

as does inheriting from list (python 2.2) :

>>> class l(list):
...     def append(self,x):
...             print "hello"
...             list.append(self,x)
... 
>>> a=l()
>>> a
[]
>>> a.append(2)
hello
>>> a
[2]



More information about the Python-list mailing list