hey... I wasn't able to find much information on these class methods other than __init__ ... could you explain what these (and maybe some others) do or perhaps just give me a link to a page that explains them:
These are used to override external functions/methods, for example:
# example of methods you can override in classes
self.amount = amount
return '$%.2f' % self.amount
return '"%s"' % str(self.amount)
a = Dollars(10)
print a # 10
print "The amount is", a # The amount is 10
print "The amount is", repr(a) # The amount is $10.00
print str(a) # "10"
There are other uses like __repr__ can be used to give a class dymanic attributes.
With __cmp__() you can override, overload, customize (whatever you want to call it) the cmp() function for the class instance. There are a bunch of these babies, all starting and ending with double underlines. I think they are also called instance methods. They can do powerful things, but also can make your code a bitch to read and comprehent.
Here is one that hijacks the iterator used by a for loop:
# operator overloading with instance method __iter__()
"""custom iterator for looping over a sequence backwards
sort of hijacks the iterator used by the for loop
uses __iter__(self) and next(self) to do the trick"""
def __init__(self, data):
self.data = data
self.index = len(data)
if self.index == 0:
self.index = self.index - 1
for c in Reverse('iterator overload'):
print c, # d a o l r e v o r o t a r e t i
Note that Reverse('iterator overload') is really a class instance!