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
Ïà¹ØÎĵµ£º
pythonµÄC¡¢c++À©Õ¹
http://blog.chinaunix.net/u3/110228/showart_2148725.html
pythonµÄÇ¿´ó²»½ö±íÏÖÔÚÆ书ÄÜÉÏ£¬¶øÇÒ»¹±íÏÖÔÚÆäÀ©Õ¹ÄÜÁ¦ÉÏ¡£
ʹÓÃC/C++ºÜÈÝÒ×±àдpythonµÄÄ£¿é£¬À©Õ¹pythonµÄ¹¦ÄÜ¡£
ͬʱ½«ÐÔÄÜÒªÇó±È½Ï¸ßµÄ´úÂëʹÓÃC/C++±àд£¬ÄܸüºÃµÄÃÖ²¹
½Å±¾ÓïÑÔÖ´ÐÐËÙ¶ÈÂýµÄȱÏÝ¡£
1. pythonµÄCÓïÑÔÀ©Õ¹
1.1 ......
ÏÂÔØ°²×°MySQLdb
http://sourceforge.net/projects/mysql-python/ ºÃÏñû¿´µ½windows°æ±¾for python2.6µÄÏÂÔØ£¬ÍøÉÏËÑË÷µ½Ò»¸ö
http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe
°²×°ºóimport MySQLdb»á³öÏÖ DeprecationWarning: the sets module is deprecated ÕâÑùÒ»¸ö¾¯¸æ£¬googleÖ®
......
python µÄÄÚǶtimeÄ£°å·Ò뼰˵Ã÷
Ò»¡¢¼ò½é
timeÄ£¿éÌṩ¸÷ÖÖ²Ù×÷ʱ¼äµÄº¯Êý
˵Ã÷£ºÒ»°ãÓÐÁ½ÖÖ±íʾʱ¼äµÄ·½Ê½:
µÚÒ»ÖÖÊÇʱ¼ä´ÁµÄ·½Ê½(Ïà¶ÔÓÚ1970.1.1 00:00:00ÒÔÃë¼ÆËãµÄÆ«ÒÆÁ¿),ʱ¼ä´ÁÊÇΩһµÄ
µÚ¶þÖÖÒÔÊý×éµÄÐÎʽ±íʾ¼´(struct_time),¹²ÓоŸöÔªËØ£¬·Ö±ð±íʾ£¬Í¬Ò»¸öʱ¼ä´ÁµÄstruct_time»áÒòΪʱÇø²»Í¬¶ø²»Í¬
year ......
Programming Python, 2nd Edition (O'Reilly)
http://www.osbbs.com/dl/Programming Python, 2nd Edition (O'Reilly).chm
ºÜÈ«ºÜ¾µäÁËpythonѧϰÈëÃÅ×ÊÁÏ
OReilly - Learning Python:
http://www.osbbs.com/dl/OReilly - Learning Python.chm
......
Ê×ÏÈתÔØÒ»¶Î¹ØÓÚÍƼöµÄѧϰpythonµÄÊé¼®£¨ÔÎÄÁ´½Ó£©£º
1.ѧϰ¡¶A Byte of Python¡·£¬ÖÐÎÄÒëÃûΪ¡¶Python¼òÃ÷½Ì³Ì£¨pdfÏÂÔصØÖ·£©¡·£¬È«ÊéÏÂÀ´Ö»ÓÐ100¶àÒ³£¬¼òµ¥¸ÉÁ·£¬Í¨Ë×Ò׶®£¬Êǹ«ÈϵÄPythonÈëÃŽ̡̳£ÎÒËäȻϵ½ÁË¡¶Python¼òÃ÷½Ì³Ì¡·pdf°æ£¬µ«ÊǾõµÃÆäÖеĴúÂë¿ÉÄÜÊÇÒòΪÒëÕßת»»ÎĵµÀàÐ͵ÄÔÒò£¬¸ñʽ±äµÃºÜÂÒ£¬ËùÒÔ× ......