Extend a class?

Alex cut_me_out at hotmail.com
Mon Sep 25 11:41:38 EDT 2000


> 	  I would like to know if I could easily create a module, say
> called futils.py, which would allow me, once loaded, to add some
> methods to various objects, such as lists. I would like to be able to
> do something like: l=["123",1,2,4] l.writeasciifile("test.dat")

Check out the UserList module.  Here's a simple example.  It uses
python2.0 features.

from UserList import UserList

class MyList(UserList):

    def evens(self):

        return [self.data[i] for i in range(0, len(self), 2)]

t = MyList(range(10))
print t.evens()

Alex.

-- 
Speak softly but carry a big carrot.




More information about the Python-list mailing list