View Single Post
Old Jun 3rd, 2006, 11:02 AM   #7
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Pretty good - Although you don't have to do the "return obj" stuff. That's only for __new__, not __init__. The difference between __new__ and __init__ is subtle, but worth knowing.

Take the following object creation code (note that Foobar has to be a new-style class) :
obj = Foobar(1, 2)
The above code is equivalent to:
obj = Foobar.__new__(Foobar, 1, 2)
obj.__init__(1, 2)
In Python, the __new__ method is the constructor. It returns a new object. Interestingly, there's no limit to what object is returned; you could return anything, though typically __new__ returns a new instance of the class. The first argument of __new__ is not "self" (ie. a reference to the object), but "cls" (ie. a reference to the calling class).

The __init__ method is the initialiser. It is called after the object is constructed, and is used to give the object its initial data. The __init__ method's return value is ignored.

Returning back to your code, it can be simplified somewhat:
class advanced_list(list):
	def __init__(self, *args):
		list.__init__(self, args)

	def len(self):
		return len(self)

	def filter(self, func):
		return filter(func, self)

	def map(self, func):
		return map(func, self)
Arevos is offline   Reply With Quote