example of python operator overloadind
And last here is the overload operators example:
# map() takes two (or more) arguments, a function and a list to apply the function to
# lambda can be put anywhere a function is expected
# map() calls lambada for every element in the self list
# since Vector has overloaded __getitem__ and __len__ definitions
# the Vector object can be considered a list
# the lambda function adds each other item to each item in the list
# note this only adds objects that can typicaly be added by python
# print statements added to show what is getting called
class Vector:
def __init__(self, data):
print "__init__"
self.data = data
def __call__(self, varA, varB):
print "__call__"
print "do something with ", varA, " and ", varB
# overload print
# repr returns a string containing a printable representation of an object
# otherwise printing a Vector object would look like:
#<__main__.Vector instance at 0x0000000017A9DF48>
def __repr__(self):
print "__repr__"
return repr(self.data)
# overload +
def __add__(self, other):
print "__add__"
return Vector(map(lambda x, y: x+y, self, other))
# overload -
def __sub__(self, other):
print "__sub__"
return Vector(map(lambda x, y: x-y, self, other))
# overload /
def __div__(self, other):
print "__div__"
return Vector(map(lambda x, y: x/y, self, other))
# overload *
def __mul__(self, other):
print "__mul__"
return Vector(map(lambda x, y: x*y, self, other))
# overload %
def __mod__(self, other):
print "__mod__"
return Vector(map(lambda x, y: x%y, self, other))
# overload []
def __getitem__(self, index):
print "__getitem__"
return self.data[index]
# overload set []
def __setitem__(self, key, item):
print "__setitem__"
self.data[key] = item
# return size to len()
def __len__(self):
print "__len__"
retur
Ïà¹ØÎĵµ£º
Ç°¼¸Ì죬СÐí¸øÎÒÒ»·ÝJavaQQµÄÔ´´úÂ룬ÓÃvim´ò¿ªÒ»¿´£¬·¢ÏÖÀïÃæµÄÖÐÎĶ¼ÊÇÂÒÂë¡£²»ÓÃ˵£¬ÓÖÊǿɶñµÄ±àÂëÎÊÌ⣬ÔÚwindowϵÄÎı¾Îļþͨ³£Ê¹ÓÃGBK»òGB18030±àÂ룬¶øÔÚLinuxÏÂutf-8±àÂëÔò´óÐÐÆäµÀ¡£´ò¿ª——Áí´æΪ¿Ï¶¨²»ÊÇÉϲߣ¬ÉÏÍøÕÒ±àÂëÅúÁ¿×ª»»¹¤¾ßÒ²²»ÊÇÔÛÇÚÀÍÓ¸ҵijÌÐòÔ±µÄ×÷·ç¡£×ÔÒѶ¯ÊÖ ......
Ò»¡¢Ê¹ÓÃPythonÐèÒªÖªµÀµÄ
ÔÚ Windows ÉÏ£¬°²×° Python ÓÐÁ½ÖÖÑ¡Ôñ¡£
1¡¢ActiveState ÖÆ×÷µÄ ActivePython ÊÇרÃÅÕë¶Ô Windows µÄ Python Ì×¼þ£¬Ëü°üº¬ÁËÒ»¸öÍêÕûµÄ Python ·¢²¼¡¢Ò»¸öÊÊÓÃÓÚPython ±à³ÌµÄ IDE ÒÔ¼°Ò»Ð© PythonµÄ Windows À©Õ¹£¬ÌṩÁËÈ«²¿µÄ·ÃÎÊ Windows APIs µÄ·þÎñ£¬ÒÔ¼° Windows×¢²á±íµÄ×¢²áÐÅÏ¢¡£ËäÈ ......
×î½ü£¬ÏëÔÚÎÒµÄYouMoney£¨http://code.google.com/p/youmoney/£©ÀïÃæÔö¼ÓÌáÈ¡Óû§²Ù×÷ϵͳ°æ±¾ÐÅÏ¢¡£±ÈÈçwindowsÓû§£¬¿ÉÄÜÒª·µ»ØWindows XP ,»òÕßWindows 2003, Æ»¹ûÓû§Ó¦¸Ã·µ»ØMac OS X 10.5.8¡£ÓÃÁ˺ܶà°ì·¨£¬°üÀ¨ÔÚmacϵͳÀïµ÷ÓÃϵͳÃüÁȡ»·¾³±äÁ¿£¬µÈµÈ¡£×îºóÎÞÒâ·¢ÏÖ£¬ÔÀ´pythonÀïÀïÃæÓиöpl ......
À´Ô´:
×÷Õß:
Áé½£
1.python ×Ö·û´®Í¨³£Óе¥ÒýºÅ£¨'...'£©¡¢Ë«ÒýºÅ£¨...£©¡¢ÈýÒýºÅ£¨...£©»ò£¨'''...'''£©°üΧ£¬ÈýÒýºÅ°üº¬µÄ×Ö·û´®¿ÉÓɶàÐÐ×é³É£¬Ò»°ã¿É±íʾ´ó¶ÎµÄÐðÊöÐÔ×Ö·û´®¡£ÔÚʹÓÃʱ»ù±¾Ã»Óвî±ð£¬
1.python
×Ö·û´®Í¨³£Óе¥ÒýºÅ£¨'...'£©¡¢Ë«ÒýºÅ£¨"..."£©¡¢ÈýÒýºÅ£¨"""... ......